Skip to content

sa_column(ForeignKey) not working #326

Description

@adpmhel24

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • 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

Activity

  1. JLHasson commented on May 3, 2022

    @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.

  2. adpmhel24 commented on May 3, 2022

    @adpmhel24
    Author

    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

  3. adpmhel24 commented on May 4, 2022

    @adpmhel24
    Author

    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.

  4. zhangbc97 commented on May 9, 2022

    @zhangbc97

    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")),
        )
    
  5. locked and limited conversation to collaborators on Aug 11, 2025
  6. converted this issue into a discussion #1480 on Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions