Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Add custom table names and then reference them in foreign keys
Browse files Browse the repository at this point in the history
This will fix the NoReferencedTableError raised by SQLAlchemy.

Closes #17
  • Loading branch information
SuyashD95 committed Feb 10, 2021
1 parent 8e1ef2e commit 2042c5d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions API/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
class UserModel(db.Model):
"""Model class defined for the User table."""

__tablename__ = 'users'

_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), unique=True, nullable=False)
password = db.Column(db.String(64), nullable=False)
Expand All @@ -34,10 +36,12 @@ def __repr__(self):
class RoomModel(db.Model):
"""Model class defined for the Room table."""

__tablename__ = 'rooms'

_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), unique=True, nullable=False)
admin_id = db.Column(db.Integer,
db.ForeignKey('user._id', onupdate='CASCADE', ondelete='CASCADE'),
db.ForeignKey('users._id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False
)
messages = db.relationship('MessageModel', backref='room', lazy=True)
Expand All @@ -50,15 +54,17 @@ def __repr__(self):
class MessageModel(db.Model):
"""Model class defined for the Message table."""

__meetingrooms__ = 'messages'

_id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, nullable=False)
sender_id = db.Column(db.Integer,
db.ForeignKey('user._id', onupdate='CASCADE', ondelete='CASCADE'),
db.ForeignKey('users._id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False
)
room_id = db.Column(db.Integer,
db.ForeignKey('room._id', onupdate='CASCADE', ondelete='CASCADE'),
db.ForeignKey('rooms._id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False
)

Expand Down

0 comments on commit 2042c5d

Please sign in to comment.