sqlmodel
sqlmodel copied to clipboard
sa_column(ForeignKey) not working
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
branch_id: Optional[int] = Field(
sa_column=Column("branch_id", Integer, ForeignKey("branch.id")),
)
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
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 named id
?
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")),
)