class Parent(db.Model):
id = db.Column(db.Integer, primary_key=True)
class Child(db.Model):
id = db.Column(db.Integer, primary_key=True)
parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))
parent = Parent()
db.session.add(parent)
child = Child()
child.parent_id = parent.id
db.session.add(child)
db.session.commit()
I want to INSERT into both parent
and child
tables inside a session considering that the parent_id
must be included in the child
table. In the moment I create the child
object, parent.id
is None.
How can I achieve that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…