When using SQLAlchemy, what is the ideal way to insert an object into a table with a column that is a foreign key and then commit it? Is there anything wrong with inserting objects with a foreign in the code below?
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)
obj.someforeignkey = another_obj
session.add(obj)
session.flush()
transaction.commit()
session.close()
return None