I handle my PostgreSQL migrations with Alembic
. This is how I create a table items
:
from alembic import op
import sqlalchemy as sa
def upgrade():
items_table = op.create_table(
"items",
sa.Column("id", UUID(as_uuid=True), primary_key=True),
sa.Column("user_id", UUID(as_uuid=True), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
),
)
I'd like to make a new migration file to add ondelete="CASCADE"
after the sa.ForeignKeyConstraint(...)
. How can I do this using sqlalchemy
? How do I drop the ForeignKeyConstraint
and create a new one? Or do I need to drop the whole table and create it again?