I think it can only be a version difference, maybe you have different versions of the libraries on the different machines. Maybe different SQLAlchemy and you hit this, #315
Spent this evening putting together a minimal viable reproduction of the error and just saw your comment. Downgrading SQLAlchemy to 1.4.35 does indeed resolve the issue.
In case it's useful, running pytest on the code below will pass on MacOS (M1) but fail on Ubuntu. There is a much smaller and cleaner code example in the issue you linked to though
fromtypingimportList, OptionalimportpytestfromfastapiimportDepends, FastAPIfromfastapi.testclientimportTestClientfromsqlmodelimportField, Relationship, Session, SQLModel, create_engine, selectfromsqlmodel.poolimportStaticPoolapp=FastAPI()
engine=create_engine("sqlite://", echo=False, connect_args={"check_same_thread": False})
defget_session():
withSession(engine) assession:
yieldsessionclassSourceBase(SQLModel):
name: Optional[str] =Field(nullable=True)
classSource(SourceBase, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
articles: List["Article"] =Relationship(back_populates="source")
classSourceRead(SourceBase):
id: intclassArticleBase(SQLModel):
title: str=Field(nullable=False)
source_id: Optional[int] =Field(default=None, foreign_key="source.id")
classArticleCreate(SQLModel):
title: str=Field(nullable=False)
source_name: str=Field(nullable=False)
classArticle(ArticleBase, table=True):
id: Optional[int] =Field(default=None, primary_key=True)
source: Source=Relationship(back_populates="articles")
classArticleGet(ArticleBase):
id: intclassArticleGetWithSource(ArticleGet):
source: Optional[SourceRead] =None@pytest.fixture(name="session")defsession_fixture():
engine=create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
SQLModel.metadata.create_all(engine)
withSession(engine) assession:
yieldsession@pytest.fixture(name="client")defclient_fixture(session: Session):
defget_session_override():
returnsessionapp.dependency_overrides[get_session] =get_session_overrideclient=TestClient(app)
yieldclientapp.dependency_overrides.clear()
@app.post("/add", response_model=ArticleGetWithSource)defadd_article(*, session: Session=Depends(get_session), payload: ArticleCreate) ->Article:
source=session.exec(select(Source).where(Source.name==payload.source_name)).first()
source_id=source.idifsourceelseNonearticle=Article(title=payload.title, source_id=source_id)
session.add(article)
session.commit()
returnsession.exec(select(Article).where(Article.id==article.id)).first()
deftest_create_article(session: Session, client: TestClient):
source=Source(name="The Way Of The Dodo")
session.add(source)
session.commit()
response=client.post("/add", json={"title": "All This Useless Beauty", "source_name": "The Way Of The Dodo"})
data=response.json()
assertresponse.status_code==200assertdata["title"] =="All This Useless Beauty"assertdata["source_id"] isnotNoneassertdata["id"] isnotNoneassertdata["source"] isnotNoneassertdata["source"]["id"] isnotNoneassertdata["source"]["name"] =="The Way Of The Dodo"assertdata["id"] ==data["source"]["id"]
Activity
antont commented on Jul 18, 2022
I think it can only be a version difference, maybe you have different versions of the libraries on the different machines. Maybe different SQLAlchemy and you hit this, #315
HenryDashwood commented on Aug 25, 2022
Spent this evening putting together a minimal viable reproduction of the error and just saw your comment. Downgrading SQLAlchemy to 1.4.35 does indeed resolve the issue.
In case it's useful, running pytest on the code below will pass on MacOS (M1) but fail on Ubuntu. There is a much smaller and cleaner code example in the issue you linked to though