-
-
Notifications
You must be signed in to change notification settings - Fork 688
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
SQLAlchemy version 1.4.36 breaks SQLModel relationships #315
Comments
We pinned to 1.4.35, in our fork of sqlmodel. Happened to have that locally as upgraded recently and hadn't had problems. Ran into this same this afternoon. Thanks for reporting! |
Same issue here, pinning to 1.4.35 resolved |
It seems related to this recent change:
https://github.com/sqlalchemy/sqlalchemy/blob/main/lib/sqlalchemy/orm/decl_api.py#L114 |
As @byrman wrote, the change of DeclarativeMeta seems to be the breaking change here.
|
Not sure if this is a sustainable fix, I don't know how to leverage
|
The SQLAlchemy maintainers confirmed that this is the issue, and also suggested a fix : sqlalchemy/sqlalchemy#7972 (reply in thread) |
The fix I suggested is backwards compatible. Shall I make a pull request or is there more to it than setting |
Let me know if this can be improved upon: #322. |
For reference, the actual error message is: AttributeError: 'SomeModelWithRelationshipAttribute' object has no attribute 'the_relationship_attribute' |
Pending resolution of fastapi/sqlmodel#315
thank you @archydeberker !! after hours of debugging i found this issue pinned the version and it works now! <3 thank you! |
* Add invitation table * invitation wip * Fix test suite * Fix test * Fix sqlmodel relationship issue See: fastapi/sqlmodel#315 * Attempt to fix mypy * Don't use transformer for testing * Remove MyPy * Black 22.3 Co-authored-by: Jeremy Fisher <jeremy@adamsfisher.me>
* Add invitation table * invitation wip * Fix test suite * Fix test * Fix sqlmodel relationship issue See: fastapi/sqlmodel#315 * Attempt to fix mypy * Don't use transformer for testing * Remove MyPy * Black 22.3 * Add in-memory option * Plane dev environment✈️ Co-authored-by: Jeremy Fisher <jeremy@adamsfisher.me>
I also ran into this issue, I have another dependency which requires SQLAlchemy > 1.4.36 which makes this quite cumbersome for me. Luckily it's just a side project which I'm using to evaluate viability of using SQLModel to reduce duplication. |
Echoing that we are also seeing this issue in our project. Pinning to |
Thanks for the report @archydeberker! 🤓 And thanks for the discussion everyone! This was solved by @byrman in #322. It will be available in the next version, SQLModel |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
I can still reproduce the issue with from sqlalchemy import ForeignKeyConstraint
from sqlalchemy.orm import RelationshipProperty, selectinload
from sqlmodel import Field, Relationship, SQLModel, select
class Parent(SQLModel, table=True):
__tablename__ = "parents"
# The keys are set up this way
parent_id: int = Field(primary_key=True)
name: str
children: list["Child"] = Relationship(
sa_relationship=RelationshipProperty(
"Child",
back_populates="parent",
)
)
class Child(SQLModel, table=True):
__tablename__ = "children"
__table_args__ = (
ForeignKeyConstraint(
["parent_id"],
[f"{Parent.__tablename__}.parent_id"],
ondelete="CASCADE",
),
)
child_id: int = Field(primary_key=True)
parent_id: int
name: str
parent: Parent = Relationship(
sa_relationship=RelationshipProperty(
"Parent",
cascade="all, delete",
back_populates="children",
)
)
stmt = select(Parent).options(selectinload(Parent.children)) Running it gives the following error:
It works fine with |
Indeed, if
Sorry I missed that path! It needs to be addressed, of course, but perhaps you can use this as a workaround:
|
@mathieu-lemay, I made a pull request that covers your case as well: #461. I still have to add a test though. |
@byrman The workaround works so I'm gonna use that. Thanks for the fix! |
First Check
👆 Not quite true - this is definitely related to SQLAlchemy!
Commit to Help
Example Code
Description
Our CI suddenly started failing, despite local SQLModel working fine. The issues turns out to be the transitive dependency on SQLAlchemy, which is weakly pinned: Github Actions pulled the latest version (
1.4.36
) and most of our tests started failing.https://github.com/sqlalchemy/sqlalchemy/releases/tag/rel_1_4_36
The problem seems to be related to how relationships are defined, but I haven't yet dug into the SQLAlchemy changes enough to understand why that is.
I'm opening this issue chiefly to help anybody else who is confused by why suddenly their tests are failing. I'm happy to help fix it if it's affecting others too.
For the time being we have just pinned
SQLAlchemy==1.4.34
in ourrequirements.txt
.Operating System
Linux, macOS
Operating System Details
Replicated locally and on Github Actions, both running in Docker
SQLModel Version
0.0.6
Python Version
3.9.10
Additional Context
We were previously running SQLAlchemy 1.4.34 locally and that works fine. Pinning to 1.4.36 breaks SQLModel.
The text was updated successfully, but these errors were encountered: