dyntastic icon indicating copy to clipboard operation
dyntastic copied to clipboard

Clarification on conditional updates of sub-attributes

Open sarflux opened this issue 1 year ago • 3 comments

would there be an easy way to update attributes based on conditions on the sub attributes?

For example if my Dyntastic object looks like so

class Foo(Dyntastic):
    __table_name__ = getenv("TABLE_NAME", None)
    __hash_key__ = getenv("PARTITION_KEY", None)
    pk: str= Field()
    data: Data | None = Field(None)

class Data(BaseModel):
    id: str| None = Field(None)

and I would like to update my data field with some arbitrary condition on my data.id field:


foo = Foo(pk="some_pk")
data_dump = Data(id="bar")
foo.update(A.data.set(data_dump), condition = (A.data.not_exists() | A.data.id.ne("foo") ) 

sarflux avatar Dec 16 '24 18:12 sarflux

It might be possible using A("data.id") to reference nested fields. Give that a try and see if it works for your use case.

nayaverdier avatar Dec 16 '24 18:12 nayaverdier

It seems to result in a no-op even if the condition is correct,

my condition statement is something like this (id is int now):

foo.update(A.data.set(data.model_dump(exclude_none=True)),
                    condition=(A("data.id").lt(data.id)),require_condition=True,
                    refresh=False)

I don't really know if I'm still doing something wrong, maybe Attr is not being set correctly for nested fields?

sarflux avatar Dec 16 '24 19:12 sarflux

I was able to confirm that this simple case works:

item.update(A("my_dict.a").set(100), condition=A("my_dict.a") == 1)
assert item.my_dict["a"] == 100

What behavior are you seeing? Since you have require_condition=True, do you see a ConditionException?

nayaverdier avatar Dec 18 '24 16:12 nayaverdier