edgedb-python
edgedb-python copied to clipboard
Updating a Pydantic model using Pydantic's `model_copy` errors when trying to save the copy
Using a partial "update" Pydantic model to directly update an existing Gel model using the Pydantic model_copy mechanism fails.
class FooUpdate(BaseModel):
name: str = "Anonymous"
def update_foo(client: gel.Client, foo_in: FooUpdate, foo: Foo):
foo = foo.model_copy(
update=foo_in.model_dump(exclude_unset=True)
)
await client.save(updated)
Will return an error like:
AttributeError: 'Foo' object has no attribute '__gel_changed_fields__'
This is the basic update via PATCH flow outlined here: https://fastapi.tiangolo.com/tutorial/body-updates/#warning-about-replacing
Unrelated, but we should make it so that you don't need to model_copy and just save the partial directly instead.