odapi/migrations/versions/e443e876bf33_add_foreign_ke...

53 lines
1.4 KiB
Python
Raw Normal View History

2020-10-26 17:05:04 +01:00
"""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