Replies: 3 comments
-
@konkyrkos Any chance you may have figured this out? |
Beta Was this translation helpful? Give feedback.
-
Would you please offer a minimal reproducible example? An example that uses sqlite and can be directly run, and print the result, and you can tell us what result you expected. I guess this is because sqlmodel dosen't validate data, so maybe #1041 could work, but I need an example to try. |
Beta Was this translation helpful? Give feedback.
-
Here is a minimal example: from sqlmodel import Field, Relationship, SQLModel
class ParentBase(SQLModel):
name: str | None
class Parent(ParentBase, table=True):
id: int | None = Field(default=None, primary_key=True)
children: list["Child"] = Relationship(back_populates="parent")
class ChildBase(SQLModel):
name: str
parentid: int | None = Field(default=None, foreign_key="parent.id")
class Child(ChildBase, table=True):
id: int | None = Field(default=None, primary_key=True)
parent: Parent = Relationship(back_populates="children")
class ChildWithParent(ChildBase):
id: int
parent: Parent
if __name__ == "__main__":
p1 = Parent(id=1, name="parent")
c1 = Child(id=1, parent=p1, name="child1")
dump = c1.model_dump()
print(dump)
c2 = ChildWithParent(id=2, parent=p1, name="child2")
dump = c2.model_dump()
print(dump) The only difference between instances of I encountered this issue over at FastAPI, and I am glad to be able to further hone down on the problem. Unless there is a necessary reason for why a field that defaults to a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
We are trying to update to pydantic v2 (from v1) and latest versions of sqlmodel and sqlalchemy. We have two models connected by a relationship. We want to get a model as a dict along with its relationships (as keys of the dict too). We use club.model_dump(inlcude={'id', 'uid', 'name', 'members'}). In the past instead of .model_dump we used .dict() with the same parameters it worked. Now model_dump method does not work and does not return the relationships as keys of the dict. We have tried various options of the method but nothing did the trick.
Any help and interest in the issue is welcome!
Thank you in advance.
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.21
Python Version
3.10.5
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions