edgedb-python icon indicating copy to clipboard operation
edgedb-python copied to clipboard

Updating a Pydantic model using Pydantic's `model_copy` errors when trying to save the copy

Open scotttrinh opened this issue 5 months ago • 2 comments

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__'

scotttrinh avatar Jun 19 '25 13:06 scotttrinh

This is the basic update via PATCH flow outlined here: https://fastapi.tiangolo.com/tutorial/body-updates/#warning-about-replacing

scotttrinh avatar Jun 19 '25 13:06 scotttrinh

Unrelated, but we should make it so that you don't need to model_copy and just save the partial directly instead.

elprans avatar Jun 19 '25 15:06 elprans