1

I have a Event model, where external_id is set to be unique.

    session1 = create_session()
    session2 = create_session()

    e1 = Event(external_id=1, headline='session1')
    session1.add(e1)
    e2 = Event(external_id=1, headline='session2')
    session2.add(e2)
    session1.commit()
    session2.commit()
    s = create_session()
    e = s.query(Event).filter_by(external_id=1).first()
    print e.headline

I am getting output "session1" with no errors, which means session2.commit failed, silently. Ultimately I would like to have choose do I want to overwrite what's in db or not. So if session2.commit() fails, I would like to choose wether to change insert to update for some cases. Anyone can help with this? Thanks.

EDITED: I found the answer. The way to do it is through a two pass mechanism: both session should add/commit row with minimal information (unique key only) both sessions should do query and get the row for update if we want one session to have priority, make sure we use lock for update for it

4

0 に答える 0