question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLAlchemy version 1.4.36 breaks SQLModel relationships

See original GitHub issue

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.

👆 Not quite true - this is definitely related to SQLAlchemy!

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import Optional

from sqlmodel import Field, Relationship, SQLModel


class City(SQLModel, table=True):
    name: str = Field(primary_key=True)
    heroes: "Hero" = Relationship(back_populates="city")

class Hero(SQLModel, table=True):
    name: str = Field(primary_key=True)
    city_name: Optional[str] = Field(default=None,foreign_key="city.name")
    city: Optional[City] = Relationship(back_populates="heroes",
                              sa_relationship_kwargs=dict(cascade="all,delete")
                              )


if __name__ == "__main__":

    gotham = City(name="Gotham")
    batman = Hero(name="Batman", city=gotham)

    assert batman.name == 'Batman' # This is fine
    assert batman.city == gotham # This now breaks

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 our requirements.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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:65
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

17reactions
tiangolocommented, Aug 27, 2022

Thanks for the report @archydeberker! 🤓

And thanks for the discussion everyone! This was solved by @byrman in https://github.com/tiangolo/sqlmodel/pull/322.

It will be available in the next version, SQLModel 0.0.7, released in the next hours. 🚀

12reactions
AlanSBPerkinscommented, Jun 16, 2022

The issue remains with SQLAlchemy version 1.4.37

Read more comments on GitHub >

github_iconTop Results From Across the Web

Release Notes - SQLModel
Fix SQLAlchemy version 1.4.36 breaks SQLModel relationships (#315). PR #322 by @byrman. Docs¶. Update docs for models for updating, ...
Read more >
sqlmodel - Codecov
Name Coverage on HEAD Change from BASE Fix typo · davidbrochart opened 5 days ago #446. 98.50% 0.73% message · GurLamba opened about 1 month...
Read more >
1.4 Changelog — SQLAlchemy 2.0 Documentation
Fixed regression caused by #7518 where changing the syntax “SHOW VARIABLES” to “SELECT @@” broke compatibility with MySQL versions older than 5.6, including ......
Read more >
Relationships API - SQLAlchemy 1.4 Documentation
When using the relationship.backref parameter, provides specific parameters to ... New in version 0.9.2: relationship.secondary works more effectively when ...
Read more >
Download - SQLAlchemy
It is based roughly on the Python version numbering scheme, ... Given a version number like "1.3.6", we can break it up into...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found