Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLModel Generator does not include foreign_keys in relationships #376

Open
2 tasks done
krr0land opened this issue Feb 26, 2025 · 0 comments
Open
2 tasks done

SQLModel Generator does not include foreign_keys in relationships #376

krr0land opened this issue Feb 26, 2025 · 0 comments
Assignees
Labels
Milestone

Comments

@krr0land
Copy link

krr0land commented Feb 26, 2025

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Sqlacodegen version

master branch as of 2025.Febr.26 11:14 UTC

SQLAlchemy version

2.0.38

RDBMS vendor

MSSQL

What happened?

The SQLModel does not include foreign_keys argument in Relationships. This resulted in a AmbiguousForeignKeysError as I have multiple columns that are foreign keys on the same table.

Here are the results on the Lines table:

Generated by SQLModelGenerator:

point_a: Optional["Points"] = Relationship(back_populates="Lines")
point_b: Optional["Points"] = Relationship(back_populates="Lines_")

Generated by DeclarativeGenerator:

point_a: Mapped['Points'] = relationship('Points', foreign_keys=[point_a_id], back_populates='Lines')
point_b: Mapped['Points'] = relationship('Points', foreign_keys=[point_b_id], back_populates='Lines_')

Manual solution for SQLModel (or something similar):

point_a: Points = Relationship(back_populates="Lines", sa_relationship_kwargs={"foreign_keys": "Lines.point_a_id"})
point_b: Points = Relationship(back_populates="Lines_", sa_relationship_kwargs={"foreign_keys": "Lines.point_b_id"})

Note: These are not exact tables I'm working with, but due to a company policy and confidentiality I cannot share that, so these are similar examples

Potential solution idea:

Use the sa_relationship arg in Relationship() with output generated with the Declarative Generator:

point_a: Optional['Points'] = Relationship(sa_relationship=relationship('Points', foreign_keys=[point_a_id], back_populates='Lines'))
point_b: Optional['Points'] = Relationship(sa_relationship=relationship('Points', foreign_keys=[point_b_id], back_populates='Lines_'))

Database schema for reproducing the bug

-- I cannot share the exact tables as per company policy, but something similar should cause the same issue

CREATE TABLE dbo.Points
(
    id int identity NOT NULL,
    CONSTRAINT PK_Points PRIMARY KEY (id)
)

CREATE TABLE dbo.Lines
(
    id int identity NOT NULL,
    point_a_id int NOT NULL CONSTRAINT FK__POINT_A__Lines__Points REFERENCES dbo.Points(id),
    point_b_id int NOT NULL CONSTRAINT FK__POINT_B__Lines__Points REFERENCES dbo.Points(id),
    CONSTRAINT PK_Lines PRIMARY KEY (id)
)
@krr0land krr0land added the bug label Feb 26, 2025
@sheinbergon sheinbergon self-assigned this Feb 26, 2025
@sheinbergon sheinbergon added this to the 3.1 milestone Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants