merge() overhaul

Issue #830 resolved
Mike Bayer repo owner created an issue

im becoming convinved that merge() is a silly method for us:

  • if you merge() an instance that has primary key attributes, merge() still treats it as a save() if there's no _instance_key attribute. this is too confusing.
  • merge() will also load the existing object from the database if the given instance does have an _instance_key. this impacts its usefulness in caching scenarios since the net SQL savings are cancelled out.

in the case of no _instance_key, we will probably have to:

  • detect that theres no _instance_key
  • check that there is a full primary key set up on the instance
  • if so, do a get() from the DB. if none exists in the DB, then its a save(). else, its a merge.

in the caching use-case, we need to retrieve the existing element in the session, or merge the cached one in...update() could almost be used here. update() also needs to raise an exception if one tries to update() the same id twice with two different instances (its broke right now, i notice its just blowing away the previous identity_map entry). i think cachers should probably not use merge() since thats not what they really want...they want something like obj = session.get_persistent_or_update(obj) which returns the existing entry if one is present in the session, otherwise just updates() the given instance and returns it.

Comments (3)

  1. Log in to comment