def update_or_create(model, event_id, **kwargs):
 was_created = False
 instance = db.session.query(model).filter_by(event_id=event_id).first()
 if instance:
  db.session.query(model).filter_by(event_id=event_id).update(kwargs)
 else:
  was_created = True
  instance = model(event_id=event_id, **kwargs)
 db.session.add(instance)
 db.session.commit()
 return (instance, was_created)

def update_or_create ( model , event_id , ** kwargs ) : was_created = False instance = db . session . query ( model ) . filter_by ( event_id = event_id ) . first ( ) if instance : db . session . query ( model ) . filter_by ( event_id = event_id ) . update ( kwargs ) else : was_created = True instance = model ( event_id = event_id , ** kwargs ) db . session . add ( instance ) db . session . commit ( ) return ( instance , was_created )
update or create an item based on event id as pk .