python 2.7 - How to Update Entity with one to many relationship in flask-sqlachemy -
i have 2 models, class parents , class child, want update attributes of these classses, suppose, have: parent_name = "p1", child_name = {"c1", "c2", "c3"}, after updating, have, parent_name = "p2", child_name = {"c1", "c2", "c3", "c4", "c5"
i researched it, did not find solution, give suggestion
class parent(db.model): __tablename__ = 'parent' id = db.column(db.integer, primary_key=true) children = relationship("child" backref='parent', passive_deletes=true) parent_name = db.column(db.string(64)) class child(base): __tablename__ = 'child' id = db.column(db.integer, primary_key=true) parent_id = column(integer, foreignkey('parent.id', ondelete='cascade')) child_name = db.column(db.string(64))
parent = session.query(parent).filter(parent.name='p1').one() parent.parent_name = 'p2' parent.children.extend([child(child_name='c4'), child(childe_name='c5')]) session.commit() first query parent session. edit it, remain in session, when commit, changes in session propagated database.
Comments
Post a Comment