First Check
Commit to Help
Example Code
from sqlmodel import SQLModel, Session, create_engine, Field
engine = create_engine("sqlite:///:memory:")
class A(SQLModel, table=True):
id: int | None = Field(primary_key=True)
x: int
def blow_up(self, session: Session):
self.x = 42
session.add(self)
class B(SQLModel):
a: A
def blow_up(self, session: Session):
self.a.blow_up(session)
if __name__ == "__main__":
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
a = A(x=1)
session.add(a)
# Placing `a` inside a `B` instance appears to copy the `a` instance,
# stripping `_sa_instance_state` from it. Is this intentional?
b = B(a=a)
b.blow_up(session)
Description
- Create a SQLModel instance (
a)
- Create a non-table SQLModel (
b) and nest a inside it
- Note that
b.a._sa_instance_state is not present, whereas a._instance_state is
- The call to
b.blow_up() will result in SQLAlchemy failing to find instance state
File "/Users/me/.virtualenvs/sqlmodel-issue-u6SafBdE/lib/python3.10/site-packages/sqlalchemy/orm/attributes.py", line 2254, in set_attribute
state, dict_ = instance_state(instance), instance_dict(instance)
AttributeError: 'A' object has no attribute '_sa_instance_state'
Operating System
macOS
Operating System Details
Monterey 12.4
SQLModel Version
0.0.6
Python Version
Python 3.10.4
Additional Context
My question is: what should the expected behavior here be?
Stepping back: nesting pydantic models like this is pretty natural; that's why I found this behavior surprising. But: perhaps nesting is not-so-natural when working with SQLModels? I'm not sure. The upshot is that you can't perform database operations on nested instances, for example, by calling b.a.some_method_that_does_database_stuff().
(The behavior is unchanged if B derives directly from pydantic.BaseModel, too.)
If the behavior we're seeing is not the desired behavior, I'm happy to contribute a PR, provided we have a clear understanding of what the right behavior should be.
Thanks for all the hard work on FastAPI, Typer, and SQLModel!
First Check
Commit to Help
Example Code
Description
a)b) and nestainside itb.a._sa_instance_stateis not present, whereasa._instance_stateisb.blow_up()will result in SQLAlchemy failing to find instance stateOperating System
macOS
Operating System Details
Monterey 12.4
SQLModel Version
0.0.6
Python Version
Python 3.10.4
Additional Context
My question is: what should the expected behavior here be?
Stepping back: nesting pydantic models like this is pretty natural; that's why I found this behavior surprising. But: perhaps nesting is not-so-natural when working with SQLModels? I'm not sure. The upshot is that you can't perform database operations on nested instances, for example, by calling
b.a.some_method_that_does_database_stuff().(The behavior is unchanged if
Bderives directly frompydantic.BaseModel, too.)If the behavior we're seeing is not the desired behavior, I'm happy to contribute a PR, provided we have a clear understanding of what the right behavior should be.
Thanks for all the hard work on FastAPI, Typer, and SQLModel!