You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
We have an ORM object to update, with that potentially involving adding, changing, or
removing
attributes. Coming from the
SQLAlchemy docs
, Session.merge() seemed like a good fit, emphasis mine:
"The state of the given instance is then copied onto the located/newly created instance. For attributes which are present on the source instance, the value is transferred to the target instance.
For mapped attributes which aren’t present on the source, the attribute is expired on the target instance, discarding its existing value
."
Adding and changing worked fine, but I wasn't seeing deletion. I checked what looked to be
a relevant test
here and saw:
# save another user with "data"
u2 = User(id=2, data="foo")
sess.add(u2)
sess.flush()
# merge User on u2's pk with
# no "data".
# value isn't whacked from the destination
# dict.
u3 = sess.merge(User(id=2))
eq_(u3.__dict__["data"], "foo")
which seems to indicate that attributes that aren't present in the source (here
User(id=2)
) should NOT be removed by
merge()
, contradicting my read on the docs.
It's possible that "mapped" means something specific in the bolded text. From remaining page context, I'd thought it meant ORM-mapped in the sense of
data
in the chunk above (which doesn't seem to be expired), but I could be wrong.
What behavior's intended here?
hey there -
the term "ORM mapped" or "mapped attributes" refers to all the attributes on your class /object which correspond to table columns or relationships. If you have another attribute like "x = 5", without any kind of Column / relationship, that's not a mapped attribute.
the word "discard" here doesn't mean "delete from the database", it just means the value will no longer be loaded on the Python side. That's what it means by "expired", which is discussed more in detail here:
https://docs.sqlalchemy.org/en/14/orm/session_basics.html#expiring-refreshing
hey there -
the term "ORM mapped" or "mapped attributes" refers to all the attributes on your class /object which correspond to table columns or relationships. If you have another attribute like "x = 5", without any kind of Column / relationship, that's not a mapped attribute.
the word "discard" here doesn't mean "delete from the database", it just means the value will no longer be loaded on the Python side. That's what it means by "expired", which is discussed more in detail here:
https://docs.sqlalchemy.org/en/14/orm/session_basics.html#expiring-refreshing