Add foreign keys to database

This commit is contained in:
coolneng 2020-10-26 17:05:04 +01:00
parent 6c4d6919d7
commit e2f17743b7
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 61 additions and 19 deletions

View File

@ -140,9 +140,9 @@ class Teams(Base):
__tablename__ = "teams"
id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey("users_id"))
user_id = Column(Integer, ForeignKey("users.id"))
team_id = Column(Enum("1", "2"))
game_id = Column(Integer, ForeignKey("games_id"))
game_id = Column(Integer, ForeignKey("games.id"))
status = Column(Integer)
created = Column(DateTime, nullable=False, server_default=func.now())
updated = Column(DateTime, nullable=True, onupdate=func.now())
@ -152,9 +152,9 @@ class UserRatings(Base):
__tablename__ = "user_ratings"
id = Column(Integer, primary_key=True, autoincrement=True)
game_id = Column(Integer, ForeignKey("games_id"))
user_id = Column(Integer, ForeignKey("users_id"))
player_id = Column(Integer, ForeignKey("users_id"))
game_id = Column(Integer, ForeignKey("games.id"))
user_id = Column(Integer, ForeignKey("users.id"))
player_id = Column(Integer, ForeignKey("users.id"))
rating = Column(String(100), nullable=False)
created = Column(DateTime, nullable=False, server_default=func.now())
updated = Column(DateTime, nullable=True, onupdate=func.now())
@ -165,8 +165,8 @@ class VenueImages(Base):
__tablename__ = "venue_images"
id = Column(Integer, primary_key=True, autoincrement=True)
venue_id = Column(Integer, ForeignKey("venues_id"))
user_id = Column(Integer, ForeignKey("users_id"))
venue_id = Column(Integer, ForeignKey("venues.id"))
user_id = Column(Integer, ForeignKey("users.id"))
image = Column(String(255))
created = Column(DateTime, nullable=False, server_default=func.now())
updated = Column(DateTime, nullable=True, onupdate=func.now())
@ -176,22 +176,12 @@ class Venues(Base):
__tablename__ = "venues"
id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey("users_id"))
user_id = Column(Integer, ForeignKey("users.id"))
address = Column(Text, nullable=False)
latitude = Column(String(100))
longitude = Column(String(100))
name = Column(String(100), nullable=False)
sports_id = Column(Integer, ForeignKey("sports_id"))
created = Column(DateTime, nullable=False, server_default=func.now())
updated = Column(DateTime, nullable=True, onupdate=func.now())
class ViewNews(Base):
__tablename__ = "view_news"
id = Column(Integer, primary_key=True, autoincrement=True)
news_id = Column(Integer)
user_id = Column(Integer, ForeignKey("users_id"))
sports_id = Column(Integer, ForeignKey("sports.id"))
created = Column(DateTime, nullable=False, server_default=func.now())
updated = Column(DateTime, nullable=True, onupdate=func.now())

View File

@ -0,0 +1,52 @@
"""add foreign keys
Revision ID: e443e876bf33
Revises: f8ef6bad794a
Create Date: 2020-10-26 14:37:21.156557
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "e443e876bf33"
down_revision = "f8ef6bad794a"
branch_labels = None
depends_on = None
def upgrade():
tables = {
"games": ["user_id", "city_id", "venue_id", "sports_id"],
"payments": ["user_id", "game_id"],
"player_availabilities": ["game_id", "player_id"],
"player_cancel_games": ["game_id", "player_id"],
"purchase_games": ["game_id", "user_id"],
"teams": ["game_id", "user_id"],
"users": ["city_id"],
"user_ratings": ["game_id", "user_id", "player_id"],
"venues": ["user_id", "sports_id"],
"venue_images": ["venue_id", "user_id"],
}
referent_tables = {
"user_id": "users",
"city_id": "cities",
"venue_id": "venues",
"sports_id": "sports",
"game_id": "games",
"player_id": "users",
}
for table, field in tables.items():
for item in field:
with op.batch_alter_table(table) as batch_op:
batch_op.create_foreign_key(
constraint_name="fk_{}".format(item),
referent_table=referent_tables[item],
local_cols=[item],
remote_cols=["id"],
)
def downgrade():
pass