I am creating a chat application in flask where you can create users and chat with others. I am storing all the information in SQLAlchemy. I was wondering if this structure for my models is okay. I am still a beginner with Flask and SQLAlchemy so I may have errors.
from . import db
from flask_login import UserMixin
user_chat = db.Table("user_chat",
db.Column("user_id", db.Integer, db.ForeignKey("user.id")),
db.Column("chat_id", db.Integer, db.ForeignKey("chat.id"))
)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
username = db.Column(db.String(150), unique=True)
password = db.Column(db.String(150))
firstName = db.Column(db.String(150))
lastName = db.Column(db.String(150))
birthday=db.Column(db.String(150))
sport = db.Column(db.String(150))
bio = db.Column(db.String(150))
chats = db.relationship("Chat", secondary=user_chat, backref='users')
class Chat(db.Model):
id = db.Column(db.Integer, primary_key=True)
messages = db.relationship("Message", backref="chat")
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(300))
username = db.Column(db.String(150))
chat_id = db.Column(db.Integer, db.ForeignKey("owner.id"))
User class is the user schema. Chat class is the schema for the chats which contains many messages which are the Message schema. Should I change anything or is this good? I am still learning sql format so I am not sure if you're allowed to have Chat be connected to two tables? Thank you!!
source https://stackoverflow.com/questions/72049174/is-this-structures-of-sql-models-okay-flask
Comments
Post a Comment