R
Railwayβ€’5mo ago
IssaKass

Flask deployment Crash

I recently deployed a Flask backend app to railway, and It crashed. This is the main.py:
# """Main script to run the Flask application."""

import os
from flask_cors import CORS
from flask import Flask, jsonify
from flask_migrate import Migrate
from config import get_config
from api.extensions import db, jwt
from api.routes import auth_bp, users_bp, projects_bp, subscriptions_bp

app = Flask(__name__)

CORS(app)

# # Load configuration based on the specified environment
flask_env = os.getenv("FLASK_ENV")
app.config.from_object(get_config(flask_env))


@app.route("/")
def index():
return jsonify(
{
"ENV": flask_env,
"DEBUG": app.config["DEBUG"],
"TESTING": app.config["TESTING"],
"SECRET_KEY": app.config["SECRET_KEY"],
"JWT_SECRET_KEY": app.config["JWT_SECRET_KEY"],
"SQLALCHEMY_DATABASE_URI": app.config["SQLALCHEMY_DATABASE_URI"],
}
)


# Initialize the database extension
db.init_app(app)

# Initialize the migration extension
Migrate(app, db)

# Initialize the JWT extension
jwt.init_app(app)

# Register the blueprints with a specified prefix
app.register_blueprint(auth_bp)
app.register_blueprint(users_bp)
app.register_blueprint(projects_bp)
app.register_blueprint(subscriptions_bp)


if __name__ == "__main__":
app.run(debug=True, port=os.getenv("PORT", default=5000))
# """Main script to run the Flask application."""

import os
from flask_cors import CORS
from flask import Flask, jsonify
from flask_migrate import Migrate
from config import get_config
from api.extensions import db, jwt
from api.routes import auth_bp, users_bp, projects_bp, subscriptions_bp

app = Flask(__name__)

CORS(app)

# # Load configuration based on the specified environment
flask_env = os.getenv("FLASK_ENV")
app.config.from_object(get_config(flask_env))


@app.route("/")
def index():
return jsonify(
{
"ENV": flask_env,
"DEBUG": app.config["DEBUG"],
"TESTING": app.config["TESTING"],
"SECRET_KEY": app.config["SECRET_KEY"],
"JWT_SECRET_KEY": app.config["JWT_SECRET_KEY"],
"SQLALCHEMY_DATABASE_URI": app.config["SQLALCHEMY_DATABASE_URI"],
}
)


# Initialize the database extension
db.init_app(app)

# Initialize the migration extension
Migrate(app, db)

# Initialize the JWT extension
jwt.init_app(app)

# Register the blueprints with a specified prefix
app.register_blueprint(auth_bp)
app.register_blueprint(users_bp)
app.register_blueprint(projects_bp)
app.register_blueprint(subscriptions_bp)


if __name__ == "__main__":
app.run(debug=True, port=os.getenv("PORT", default=5000))
==> crashed at line 35: db.init_app(app)
135 Replies
Percy
Percyβ€’5mo ago
Project ID: c6894929-c040-4842-8c83-4516d57a14fe
IssaKass
IssaKassβ€’5mo ago
c6894929-c040-4842-8c83-4516d57a14fe
IssaKass
IssaKassβ€’5mo ago
Locally, its working
Brody
Brodyβ€’5mo ago
my initial thoughts are to ask you if you have setup all those environment variables in railway too?
IssaKass
IssaKassβ€’5mo ago
No description
IssaKass
IssaKassβ€’5mo ago
The debug, testing, and other ones are inside a config.py
Brody
Brodyβ€’5mo ago
for example you use SQLALCHEMY_DATABASE_URI in your code, but on railway you are using DATABASE_URL are you perhaps using DATABASE_URL in the config.py file instead?
IssaKass
IssaKassβ€’5mo ago
yes: SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
King Jahad
King Jahadβ€’5mo ago
his PORT is not setup either, Issakass i thought i told you about that.
Brody
Brodyβ€’5mo ago
doesn't need to be
IssaKass
IssaKassβ€’5mo ago
the port has no problems
Brody
Brodyβ€’5mo ago
they are using gunicorn and that will use the automatically injected PORT variable that railway injects during runtime
King Jahad
King Jahadβ€’5mo ago
ah I see then int(os.getenv())
Brody
Brodyβ€’5mo ago
nope, that's not the issue either
IssaKass
IssaKassβ€’5mo ago
I have a configuration file, it has 3 config classes, for Development, Testing and Production. I am setting up app.config for the sepecific environment,
Brody
Brodyβ€’5mo ago
issa, can you please edit your original message to wrap your code in triple backticks? please edit the original message
IssaKass
IssaKassβ€’5mo ago
this is from postman:
No description
Brody
Brodyβ€’5mo ago
yeah that's the html page from railway that is shown when your app does not respond to a request, in this case because the app has crashed
IssaKass
IssaKassβ€’5mo ago
yes, it crashed do you want also to share the config.py?
Brody
Brodyβ€’5mo ago
can you send your config.py file?
IssaKass
IssaKassβ€’5mo ago
"""Configuration settings for the Flask application."""

import os
from secrets import token_hex
from dotenv import load_dotenv

load_dotenv()


class Config:
"""Base configuration class."""

DEBUG = False
TESTING = False
SECRET_KEY = "1784226bc0e01c0b47699430b5fc62e3800b2336b7d852e17e20bf40da353673"
JWT_SECRET_KEY = "b2a3f61c61cd81baafb9d1a12bb61e4e9c594d043206ce938ef840a3884d9e19"


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "sqlite:///financial.db"


class TestingConfig(Config):
DEBUG = False
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///test_financial.db"


class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
SECRET_KEY = os.getenv("SECRET_KEY", token_hex(32))
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", token_hex(32))


CONFIG = {
"default": DevelopmentConfig,
"development": DevelopmentConfig,
"testing": TestingConfig,
"production": ProductionConfig,
}


def get_config(environment="default"):
"""Get configuration based on the specified environment."""

return CONFIG[environment]
"""Configuration settings for the Flask application."""

import os
from secrets import token_hex
from dotenv import load_dotenv

load_dotenv()


class Config:
"""Base configuration class."""

DEBUG = False
TESTING = False
SECRET_KEY = "1784226bc0e01c0b47699430b5fc62e3800b2336b7d852e17e20bf40da353673"
JWT_SECRET_KEY = "b2a3f61c61cd81baafb9d1a12bb61e4e9c594d043206ce938ef840a3884d9e19"


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "sqlite:///financial.db"


class TestingConfig(Config):
DEBUG = False
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///test_financial.db"


class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
SECRET_KEY = os.getenv("SECRET_KEY", token_hex(32))
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", token_hex(32))


CONFIG = {
"default": DevelopmentConfig,
"development": DevelopmentConfig,
"testing": TestingConfig,
"production": ProductionConfig,
}


def get_config(environment="default"):
"""Get configuration based on the specified environment."""

return CONFIG[environment]
When developing, its working well! All my problem is when deploying
Brody
Brodyβ€’5mo ago
that is a good to know, but keep in mind that does not rule out a code issue, this is most definitely a code issue
IssaKass
IssaKassβ€’5mo ago
I tried to deploy railway flask template, it works. But adding all these extensions and configurations, it crashes
Brody
Brodyβ€’5mo ago
yes this is a code issue, I'm still thinking on what could be wrong though
King Jahad
King Jahadβ€’5mo ago
File "/app/main.py", line 35, in <module> db.init_app(app) return int(port) ValueError: invalid literal for int() with base 10: ''
IssaKass
IssaKassβ€’5mo ago
a very weird error I want also to connect it to postgresql service, but haven't tried
Brody
Brodyβ€’5mo ago
can you show us the code in db.init_app
IssaKass
IssaKassβ€’5mo ago
you mean that extensions.py that contains db?
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager

db = SQLAlchemy()
jwt = JWTManager()
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager

db = SQLAlchemy()
jwt = JWTManager()
anyone here??
King Jahad
King Jahadβ€’5mo ago
I think your database url has some issues. Can you confirm it has port value in the string?
IssaKass
IssaKassβ€’5mo ago
the database is working locally very well
King Jahad
King Jahadβ€’5mo ago
I understand Most probably it's your database string. Please confirm there's a port value in it.
IssaKass
IssaKassβ€’5mo ago
which one?
"""Configuration settings for the Flask application."""

import os
from secrets import token_hex
from dotenv import load_dotenv

load_dotenv()


class Config:
"""Base configuration class."""

DEBUG = False
TESTING = False
SECRET_KEY = "1784226bc0e01c0b47699430b5fc62e3800b2336b7d852e17e20bf40da353673"
JWT_SECRET_KEY = "b2a3f61c61cd81baafb9d1a12bb61e4e9c594d043206ce938ef840a3884d9e19"


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "sqlite:///financial.db"


class TestingConfig(Config):
DEBUG = False
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///test_financial.db"


class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
SECRET_KEY = os.getenv("SECRET_KEY", token_hex(32))
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", token_hex(32))


CONFIG = {
"default": DevelopmentConfig,
"development": DevelopmentConfig,
"testing": TestingConfig,
"production": ProductionConfig,
}


def get_config(environment="default"):
"""Get configuration based on the specified environment."""

return CONFIG[environment]
"""Configuration settings for the Flask application."""

import os
from secrets import token_hex
from dotenv import load_dotenv

load_dotenv()


class Config:
"""Base configuration class."""

DEBUG = False
TESTING = False
SECRET_KEY = "1784226bc0e01c0b47699430b5fc62e3800b2336b7d852e17e20bf40da353673"
JWT_SECRET_KEY = "b2a3f61c61cd81baafb9d1a12bb61e4e9c594d043206ce938ef840a3884d9e19"


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "sqlite:///financial.db"


class TestingConfig(Config):
DEBUG = False
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///test_financial.db"


class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
SECRET_KEY = os.getenv("SECRET_KEY", token_hex(32))
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", token_hex(32))


CONFIG = {
"default": DevelopmentConfig,
"development": DevelopmentConfig,
"testing": TestingConfig,
"production": ProductionConfig,
}


def get_config(environment="default"):
"""Get configuration based on the specified environment."""

return CONFIG[environment]
or in the railway?
King Jahad
King Jahadβ€’5mo ago
In the railway environment DATABASE_URL
IssaKass
IssaKassβ€’5mo ago
I want the postgresql service to work with the backend service How to share variables between backend and postgresql?
King Jahad
King Jahadβ€’5mo ago
I want you to check the DATABASE_URL which is setup in environment variables and confirm that it contains PORT
IssaKass
IssaKassβ€’5mo ago
no postgresql://:@:/
King Jahad
King Jahadβ€’5mo ago
How are you connecting postgres then? Where is your postgres service hosted?
IssaKass
IssaKassβ€’5mo ago
I haven't yet tried to connect to postgrs in railway this is my app:
IssaKass
IssaKassβ€’5mo ago
No description
King Jahad
King Jahadβ€’5mo ago
Alright Go to postgres service and copy it's PRIVATE_URL and put it in your backend DATABASE_URL
IssaKass
IssaKassβ€’5mo ago
why are you angryπŸ˜† should I copy also PGUSER, PGPORT,...
King Jahad
King Jahadβ€’5mo ago
Did I say something wrong?
Brody
Brodyβ€’5mo ago
we never want to copy paste credentials from other services like that, always use reference variables, as shown here https://docs.railway.app/guides/variables#referencing-another-services-variable
IssaKass
IssaKassβ€’5mo ago
so what to do now?
Brody
Brodyβ€’5mo ago
please look at that docs link for how you want to setup your database url service variable
IssaKass
IssaKassβ€’5mo ago
i referenced it
Brody
Brodyβ€’5mo ago
can you show me how you've done that?
IssaKass
IssaKassβ€’5mo ago
the db url referenced from psql
Brody
Brodyβ€’5mo ago
maybe let's not send that in a public chat
IssaKass
IssaKassβ€’5mo ago
ok
Brody
Brodyβ€’5mo ago
are you using a reference variable for your DATABASE_URL service variable on railway
IssaKass
IssaKassβ€’5mo ago
yes. I added a new variable in Backend service, and added reference from psql
IssaKass
IssaKassβ€’5mo ago
No description
Brody
Brodyβ€’5mo ago
show me the raw value
IssaKass
IssaKassβ€’5mo ago
DATABASE_URL=${{Postgres.DATABASE_URL}} It works well now. There is one thing only now
IssaKass
IssaKassβ€’5mo ago
No description
IssaKass
IssaKassβ€’5mo ago
how to load my database?? and keep updating it without affecting its data
Brody
Brodyβ€’5mo ago
looks like you need to run a migration
IssaKass
IssaKassβ€’5mo ago
Yes, that right but before that, I want to remove all existing data
Brody
Brodyβ€’5mo ago
well you have no tables so therefore no data
IssaKass
IssaKassβ€’5mo ago
In development and testing, I am working with Sqlite should i create tables manually? Or I can import from existing ones
Brody
Brodyβ€’5mo ago
you should run a migration
IssaKass
IssaKassβ€’5mo ago
does the migration works eveny if developing with sqlite ?? or the migration doesn't depend on any database, it depends on sqlalchemy and models??
Brody
Brodyβ€’5mo ago
that's correct what command would you run to perform the migration?
IssaKass
IssaKassβ€’5mo ago
flask db migrate -m "..."
flask db migrate -m "..."
followed by
flask db upgrade
flask db upgrade
Brody
Brodyβ€’5mo ago
and where do those commands pull the database credentials from? since they connect to the database after all, they need to get the database credentials from somewhere
IssaKass
IssaKassβ€’5mo ago
couldnt understand correctly
Brody
Brodyβ€’5mo ago
not too sure how I could really say that in a more easy to understand way
IssaKass
IssaKassβ€’5mo ago
for example if I type flask to the terminal: Commands:... db Perform database migrations. routes Show the routes for the app. run Run a development server. shell Run a shell in the app context. flask db for performing db migrations
Brody
Brodyβ€’5mo ago
yes I get that, but the db command connects to the database, where is it getting the database credentials so it knows what database to connect and authenticate with
IssaKass
IssaKassβ€’5mo ago
I think from config.py
"""Configuration settings for the Flask application."""

import os
from secrets import token_hex
from dotenv import load_dotenv

load_dotenv()


class Config:
"""Base configuration class."""

DEBUG = False
TESTING = False
SECRET_KEY = "1784226bc0e01c0b47699430b5fc62e3800b2336b7d852e17e20bf40da353673"
JWT_SECRET_KEY = "b2a3f61c61cd81baafb9d1a12bb61e4e9c594d043206ce938ef840a3884d9e19"


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "sqlite:///financial.db"


class TestingConfig(Config):
DEBUG = False
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///test_financial.db"


class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
SECRET_KEY = os.getenv("SECRET_KEY", token_hex(32))
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", token_hex(32))


CONFIG = {
"default": DevelopmentConfig,
"development": DevelopmentConfig,
"testing": TestingConfig,
"production": ProductionConfig,
}


def get_config(environment="default"):
"""Get configuration based on the specified environment."""

return CONFIG[environment]
"""Configuration settings for the Flask application."""

import os
from secrets import token_hex
from dotenv import load_dotenv

load_dotenv()


class Config:
"""Base configuration class."""

DEBUG = False
TESTING = False
SECRET_KEY = "1784226bc0e01c0b47699430b5fc62e3800b2336b7d852e17e20bf40da353673"
JWT_SECRET_KEY = "b2a3f61c61cd81baafb9d1a12bb61e4e9c594d043206ce938ef840a3884d9e19"


class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = "sqlite:///financial.db"


class TestingConfig(Config):
DEBUG = False
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///test_financial.db"


class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL")
SECRET_KEY = os.getenv("SECRET_KEY", token_hex(32))
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", token_hex(32))


CONFIG = {
"default": DevelopmentConfig,
"development": DevelopmentConfig,
"testing": TestingConfig,
"production": ProductionConfig,
}


def get_config(environment="default"):
"""Get configuration based on the specified environment."""

return CONFIG[environment]
here i am specifiying SQLALCHEMY_DATABASE_URI for each environment
Brody
Brodyβ€’5mo ago
please confirm that, having to guess on such things is going to make your job as a developer exponentially harder, and my job helping you even harder
IssaKass
IssaKassβ€’5mo ago
here, I am providing SQLALCHEMY_DATABASE_URI, and the migrations should work on it
IssaKass
IssaKassβ€’5mo ago
No description
Brody
Brodyβ€’5mo ago
I can see that, but is that what database url the flask db command will use to connect to and run the migrations?
IssaKass
IssaKassβ€’5mo ago
yes
flask db init
flask db init
creates a migration directory
Brody
Brodyβ€’5mo ago
okay so have you tried to run these commands locally?
IssaKass
IssaKassβ€’5mo ago
yes it creates a migrations folder locally
Brody
Brodyβ€’5mo ago
are you running in production locally so that the migrations are ran on railways postgres database?
IssaKass
IssaKassβ€’5mo ago
I worked with this, when defining new models or changes to db, then I migrate and upgrade it no, the migrations are ran locally
Brody
Brodyβ€’5mo ago
you need to perform the migrations on the postgres database on railway you may need to run your app in production locally so that your code connects to the database on railway instead of sqlite
IssaKass
IssaKassβ€’5mo ago
I'll tru try*
Brody
Brodyβ€’5mo ago
did you know you can edit your messages on discord
IssaKass
IssaKassβ€’5mo ago
so I may change FLASK_ENV to production, and run migration??
Brody
Brodyβ€’5mo ago
that sounds correct to me
IssaKass
IssaKassβ€’5mo ago
no, I'm new to using discord sorry
Brody
Brodyβ€’5mo ago
no worries, I am new to discord too believe it or not lol
IssaKass
IssaKassβ€’5mo ago
I am getting: RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set.
Brody
Brodyβ€’5mo ago
have you set it in your .env file
IssaKass
IssaKassβ€’5mo ago
so I must set Database_url in .env?
Brody
Brodyβ€’5mo ago
correct
IssaKass
IssaKassβ€’5mo ago
so whenever I want to change things, I change the .env? I was thinking that .env must be static and not changes
IssaKass
IssaKassβ€’5mo ago
migrated successfully
No description
IssaKass
IssaKassβ€’5mo ago
should i add references to them in backend??
Brody
Brodyβ€’5mo ago
did you touch the service variables on the database service??
IssaKass
IssaKassβ€’5mo ago
no
Brody
Brodyβ€’5mo ago
railway is telling you that you have removed one of those variables
IssaKass
IssaKassβ€’5mo ago
all are available
Brody
Brodyβ€’5mo ago
okay either way it doesn't really matter, the data tab is not reliable
IssaKass
IssaKassβ€’5mo ago
they are shown
Brody
Brodyβ€’5mo ago
please confirm the tables are there with software like dbgate
IssaKass
IssaKassβ€’5mo ago
i was logged out I logged in an the tables exists
Brody
Brodyβ€’5mo ago
okay good enough so yeah tables are there, job done
IssaKass
IssaKassβ€’5mo ago
so now the app should work? Thank you very much
Brody
Brodyβ€’5mo ago
I assume so, I have no way to say for certain
IssaKass
IssaKassβ€’5mo ago
Thank you!!!
Brody
Brodyβ€’5mo ago
you have the flask environment set to production on railway right?
IssaKass
IssaKassβ€’5mo ago
Yes can I set free domain also?
Brody
Brodyβ€’5mo ago
of course that's done in the service settings
IssaKass
IssaKassβ€’5mo ago
show some custom domain for frontend and backend example
Brody
Brodyβ€’5mo ago
do you own your own domain name?
IssaKass
IssaKassβ€’5mo ago
no I tried a POST request to the api and got 500 server error, why? Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Brody
Brodyβ€’5mo ago
what do the deployment logs say
IssaKass
IssaKassβ€’5mo ago
2024-01-15 17:28:54.528 UTC [9698] ERROR: relation "users" does not exist at character 217 could it be due some conflict between sqlalchmey and psql?
Brody
Brodyβ€’5mo ago
do you have any users in the database?
IssaKass
IssaKassβ€’5mo ago
which one? the users table is empty
Brody
Brodyβ€’5mo ago
seems like your code doesnt know what to do when the users table is empty but now this thread is divulging more into coding help, and unfortunately thats not something we can really offer here
IssaKass
IssaKassβ€’5mo ago
from sqlalchemy.sql import func
from flask import url_for
from werkzeug.security import generate_password_hash, check_password_hash
from api.extensions import db


class User(db.Model):
"""User Model Class"""

__tablename__ = "users"

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String, unique=True, nullable=False)
password_hash = db.Column(db.String, nullable=False)
created_at = db.Column(db.DateTime, default=func.now())
updated_at = db.Column(db.DateTime, default=func.now(), onupdate=func.now())

projects = db.relationship(
"project.Project", backref="user_projects", cascade="all, delete-orphan"
)
subscriptions = db.relationship(
"subscription.Subscription",
backref="user_subscriptions",
cascade="all, delete-orphan",
)

def __init__(self, username, email, password):
"""User Constructor Method"""

self.username = username
self.email = email
self.set_password(password)

def __repr__(self):
"""Representation Method"""

return f"<User '{self.username}'>"

def set_password(self, password):
"""Set Password Method"""

self.password_hash = generate_password_hash(password)

def check_password(self, password):
"""Check Password Method"""

return check_password_hash(self.password_hash, password)

def serialize(self):
"""Serialize Method"""

return {
"id": self.id,
"username": self.username,
"email": self.email,
"created_at": self.created_at,
"updated_at": self.updated_at,
"projects_url": url_for("users.get_user_projects", pk=self.id),
"subscriptions_url": url_for("users.get_user_subscriptions", pk=self.id),
}
from sqlalchemy.sql import func
from flask import url_for
from werkzeug.security import generate_password_hash, check_password_hash
from api.extensions import db


class User(db.Model):
"""User Model Class"""

__tablename__ = "users"

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String, unique=True, nullable=False)
password_hash = db.Column(db.String, nullable=False)
created_at = db.Column(db.DateTime, default=func.now())
updated_at = db.Column(db.DateTime, default=func.now(), onupdate=func.now())

projects = db.relationship(
"project.Project", backref="user_projects", cascade="all, delete-orphan"
)
subscriptions = db.relationship(
"subscription.Subscription",
backref="user_subscriptions",
cascade="all, delete-orphan",
)

def __init__(self, username, email, password):
"""User Constructor Method"""

self.username = username
self.email = email
self.set_password(password)

def __repr__(self):
"""Representation Method"""

return f"<User '{self.username}'>"

def set_password(self, password):
"""Set Password Method"""

self.password_hash = generate_password_hash(password)

def check_password(self, password):
"""Check Password Method"""

return check_password_hash(self.password_hash, password)

def serialize(self):
"""Serialize Method"""

return {
"id": self.id,
"username": self.username,
"email": self.email,
"created_at": self.created_at,
"updated_at": self.updated_at,
"projects_url": url_for("users.get_user_projects", pk=self.id),
"subscriptions_url": url_for("users.get_user_subscriptions", pk=self.id),
}
this is the user model, could the error because of the relations between tables?
Brody
Brodyβ€’5mo ago
im sorry but as stated above, we dont really offer coding help here unfortunately, maybe ask in a sqlalchemy forum?
IssaKass
IssaKassβ€’5mo ago
Ok, i'll try. Thank you!
Brody
Brodyβ€’5mo ago
your app is running on railway now, you just have to work out a few kinks in your code!
IssaKass
IssaKassβ€’5mo ago
Thanks!
Brody
Brodyβ€’5mo ago
no problem!
IssaKass
IssaKassβ€’5mo ago
Hello I have a very small question
Brody
Brodyβ€’5mo ago
hello
IssaKass
IssaKassβ€’5mo ago
do you remember when I explicitly added FLASK_ENV to backend??
Brody
Brodyβ€’5mo ago
locally?
IssaKass
IssaKassβ€’5mo ago
no, I added it explicitly to railway variables and use it like: flask_env = os.getenv("FLASK_ENV")
Brody
Brodyβ€’5mo ago
yeah?
IssaKass
IssaKassβ€’5mo ago
I run the command: railway variables and got:
β•‘ RAILWAY_ENVIRONMENT β”‚ production β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_ENVIRONMENT_ID β”‚ daa91003-e480-4307-85bd-6bbe0ac916e5 β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_ENVIRONMENT_NAME β”‚ production β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PRIVATE_DOMAIN β”‚ backend.railway.internal β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PROJECT_ID β”‚ c6894929-c040-4842-8c83-4516d57a14fe β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PROJECT_NAME β”‚ financial-app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PUBLIC_DOMAIN β”‚ flask-production-81aa.up.railway.app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_BACKEND_URL β”‚ flask-production-81aa.up.railway.app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_FRONTEND_URL β”‚ react-frontend-production-48e6.up.railway.app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_ID β”‚ 5ea72b4b-b3ff-43e0-b012-8a7ae7642302 β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_NAME β”‚ Backend β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_STATIC_URL β”‚ flask-production-81aa.up.railway.app β•‘
β•‘ RAILWAY_ENVIRONMENT β”‚ production β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_ENVIRONMENT_ID β”‚ daa91003-e480-4307-85bd-6bbe0ac916e5 β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_ENVIRONMENT_NAME β”‚ production β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PRIVATE_DOMAIN β”‚ backend.railway.internal β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PROJECT_ID β”‚ c6894929-c040-4842-8c83-4516d57a14fe β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PROJECT_NAME β”‚ financial-app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_PUBLIC_DOMAIN β”‚ flask-production-81aa.up.railway.app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_BACKEND_URL β”‚ flask-production-81aa.up.railway.app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_FRONTEND_URL β”‚ react-frontend-production-48e6.up.railway.app β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_ID β”‚ 5ea72b4b-b3ff-43e0-b012-8a7ae7642302 β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_SERVICE_NAME β”‚ Backend β•‘
║──────────────────────────────────────────────────────────────────────────────║
β•‘ RAILWAY_STATIC_URL β”‚ flask-production-81aa.up.railway.app β•‘
and more also is it better to use RAILWAY_ENVIRONMENT instead??
Brody
Brodyβ€’5mo ago
no i would stick with the covenantal FLASK_ENV since you arent using railways environment system anyway
IssaKass
IssaKassβ€’5mo ago
ok, thanks!
Brody
Brodyβ€’5mo ago
no prob!