Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
23 stars 4 forks source link

Field depends #174

Open Jaymon opened 8 months ago

Jaymon commented 8 months ago

Ok, the idea would be you could do something like this:

one = Field(str)
two = Field(str)

three = Field(str, depends=["one", "two"])

That will cause Schema.fields to order the fields so three comes after one and two. Likewise, Field.default and Field.fset would be changed to check depends and call a new Field.dget(orm, default_value) method to get the value.

This isn't perfect and I think there might some code paths I'm missing.

What prompted this was email addresses, I wanted to set a hash value when the email address was set, so I modified the address's fset method to set the hash value. The problem was when creating a new Orm instance, Orm.__init__ calls Field.fdefault which would set the fields passed into __init__ to fields["hash"] = None and then it would get passed to Orm.modify which called address's fset method before seeing the fields["hash"] value, which correctly set the value in hash, but then as Orm.modify continued to iterate through fields it eventually got to the hash key and value and then hash got set to None, wiping out the value the address field had previously set hash to.

Jaymon commented 8 months ago

This was a comment in a models file I've been working on:

# TODO @address_hash.setter("address") meaning that it will call
# address_hash.fset(orm, "<VALUE RETURNED FROM ADDRESS FSET>"), it's
# basically a way to say address_hash depends on the value of address. Or we
# could have a .dset method that stands for "depends fset"