If you do not use SQLAlchemy relationships on your ORM objects, you must manually use foreign keys. This means that you need to first create the parent object, return its primary key from the database and use this key in the child foreign key:
def retrieve_objects(): session = DBSession() return session.query(SomeClass).all() def insert_objects(): session = DBSession() for obj in retrieve_objects(): another_obj = AnotherClass(somefield=0) session.add(another_obj) session.flush()
source share