sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

sa_column(ForeignKey) not working

Open adpmhel24 opened this issue 2 years ago • 4 comments

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

adpmhel24 avatar May 03 '22 04:05 adpmhel24

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.

JLHasson avatar May 03 '22 17:05 JLHasson

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.

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

adpmhel24 avatar May 03 '22 22:05 adpmhel24

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. Screen Shot 2022-05-04 at 1 52 55 PM

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.

adpmhel24 avatar May 04 '22 05:05 adpmhel24

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")),
    )

zhangbc97 avatar May 09 '22 03:05 zhangbc97