Repository navigation
sa_column(ForeignKey) not working #326
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on May 3, 2022 Your code looks correct except I don't believe you need the
"branch_id"parameter in Column. Also ensure that the branch table has a parameter just namedid?In general it's hard to debug issues like this with such little info, you should provide more info about the class that contains this field as well as the class you are linking to, or a minimal example that reproduces the issue not using your code.
Your code looks correct except I don't believe you need the
"branch_id"parameter in Column. Also ensure that the branch table has a parameter just namedid?In general it's hard to debug issues like this with such little info, you should provide more info about the class that contains this field as well as the class you are linking to, or a minimal example that reproduces the issue not using your code.
When I run alembic there's no foreignkey in branch_id but when i use foreign_key parameter of the field, it will add the fk_contraints
Your code looks correct except I don't believe you need the
"branch_id"parameter in Column. Also ensure that the branch table has a parameter just namedid?In general it's hard to debug issues like this with such little info, you should provide more info about the class that contains this field as well as the class you are linking to, or a minimal example that reproduces the issue not using your code.
same to this screenshot it removes my foreignkey when I use sa_column=Column(ForeignKey).
But if i use foreign_key in Field class, fk was added. but when when I use Column it removes my foreignkey, I just to add onupdate="CASCADE" to my column.
The branch_id Field should be set in the model which "table=True"
class Model(SQLModel, table=True): branch_id: Optional[int] = Field( sa_column=Column("branch_id", Integer, ForeignKey("branch.id")), )Reacted by Toni Alatalo, farahats9, x.hw, Manish Vidyasagar and Krishna PraveenReacted by Armando Salazar- locked and limited conversation to collaborators
on Aug 11, 2025

First Check
Commit to Help
Example Code
Description
How to use sa_column with foreignkey?
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.9.3
Additional Context
No response