class Parent(B):
__tablename__ = 'parent'
id = (Integer, primary_key=True)
childrens = ("Child")
class Child(B):
__tablename__ = 'child'
id = (Integer, primary_key=True)
parent_id = (Integer, ForeignKey('parent.id'))
def save(self, parent):
parent.childrens.append(self)
db.session.add(parent)
db.session.add(self)
db.session.commit()
使用save方法后,调用 parent.childrens.all() 为空 表明未存储到数据库..why?
人肉调试一小时后..发现某个细节出错了..现在已经搞定了
Here actually db.session.add(parent) and then commit, child and parent are all entered. Judging from your code, it should still be native sqlalchemy, and backref is not configured. I don’t know if this is the problem here.
I wrote an example for you to refer to.
https://gitcafe.com/greatghoul/sqlalchemy-samples