NB: When using SQLModel + Alembic, make sure ForeignKeys are named! #1213
thomasborgen
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When using alembic to create migrations from sqlmodel Alembic does not automatically add names to ForeignKey constraints and other constraints.
TLDR
Setup sqlalchemy Metadata with naming conventions so that foreign keys and other constraints can be referred to in later migrations. Otherwise many FKs can't be changed or removed.
Longer version
Why is this important?
Well if you have a model with more than 1 FK like
This creates an alembic migration that looks something like:
ForeignKeyConstraint
has a keyword argumentname: _ConstraintNameArgument = None,
This is not set by default in SQLModel models.So if I want to change a FK or even if i want to remove a FK i can't!! Because there is no name sql can refer to when deleting it.
This can cause so many issues down the line. Fix is in the TLDR, add sqlalchemy naming conventions.
Given how userfriendly SQLModel is and how easy it is to work with because it hides a lot of complexity I think that SQLModel should define these names by default but also make it easy to set up custom ones with
ConfigDict()
. Maybe having an argument inRelationship
calledname
could also make sense.Beta Was this translation helpful? Give feedback.
All reactions