Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
should construct the links *after* the object has been added to the s…
Browse files Browse the repository at this point in the history
…ession, otherwise no guarantee that linked objects are in the same session as the object.
  • Loading branch information
mfrasca committed Jan 7, 2016
1 parent 0a28aac commit 57e2daa
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions bauble/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ def retrieve_or_create(cls, session, keys,

keys.update(extradict)

# early construct object before building links
if not is_in_session and create:
logger.debug("going to create new %s with %s" % (cls, keys))
result = cls(**keys)
session.add(result)

# or possibly reuse existing object
if is_in_session and update:
result = is_in_session

## completing the task of building the links
logger.debug("links? %s, %s" % (cls.link_keys, keys.keys()))
for key in cls.link_keys:
Expand All @@ -572,20 +582,14 @@ def retrieve_or_create(cls, session, keys,
obj = construct_from_dict(session, d)
keys[key] = obj

if is_in_session and update:
result = is_in_session
logger.debug("going to update %s with %s" % (result, keys))
if 'id' in keys:
del keys['id']
for k, v in keys.items():
if v is not None:
setattr(result, k, v)
logger.debug('returning updated existing %s' % result)
return result

logger.debug("going to create new %s with %s" % (cls, keys))
result = cls(**keys)
session.add(result)
logger.debug("going to update %s with %s" % (result, keys))
if 'id' in keys:
del keys['id']
for k, v in keys.items():
if v is not None:
setattr(result, k, v)
logger.debug('returning updated existing %s' % result)

session.flush()

logger.debug('returning new %s' % result)
Expand Down

0 comments on commit 57e2daa

Please sign in to comment.