I'm trying to save to two tables. Let say I need to insert to tbl_1 first and use the generated id to be inserted to tbl_2 then after the insert I will call the commit command. On my current approved its like
tbl_1.add(data1)
tbl_1.commit()
-- Retrieve the ID from tbl_1
tbl2.add(tbl_1.id, data2)
tbl2.commit()
The problem with this approach is if tbl2.add fails I need to delete what was added to tbl_1. And I don't like having to do delete action since my table id incrementation will be messy. What I'm thinking is like this
tbl_1.add(data1)
tbl2.add(tbl_1.id, data2)
tbl2.commit()
Any idea on how to achieve this using sqlalchemy?
***** Edit ****** tbl_1 and tbl2 are the same session object of sqlalchemy. I tried setting the autocommit to True and it works what I did was
tbl_1.add(data1)
tbl_1.flush()
tbl2.add(tbl_1.id, data2)
tbl2.commit()
Are there any problems setting autocommit to True or False?