python - Flask-sqlalchemy 一对多存储问题
巴扎黑
巴扎黑 2017-04-17 11:37:09
0
1
322
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?

人肉调试一小时后..发现某个细节出错了..现在已经搞定了

巴扎黑
巴扎黑

reply all(1)
小葫芦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!