sql server - python sqlalchemy - can't get cascade delete to work - in mssql manager -
i have code, thats been taken sqlalchemy site
class order(base): __tablename__ = 'order' id = column(integer, sequence('tri_id_seq'), primary_key=true) text = column(string(1024), nullable=false) items = relationship("item", cascade="save-update, delete-orphan, merge, delete", backref="parent") class item(base): __tablename__ = 'item' id = column(integer, sequence('tri_id_seq'), primary_key=true) text = column(string(1024), nullable=false) parent_id = column(integer, foreignkey('order.id'))
i want deletes order cascade down , delete items well. in code:
# test insert/delete - save data mssql server i1 = item(text="item one") i2 = item(text="item two") o = order(text="one", items=[i1, i2]) session.add(o) session.commit() session.delete(o) # delete # tests make sure items/order gone .... session.commit()
this works ok.
but if try , delete order in ms sql management studio. i.e.
delete [dbo].[order] id = 1
i error "the delete statement conflicted reference constraint fk__item__parent_id_1d00044f" conflict error blah blah....
i guess theres missing on relationship definitions can't see it.
any help/thoughts?
ta.
class comment(base): __tablename__ = 'comments' id = column(integer, primary_key=true) user_id = column(integer, foreignkey("users.id", ondelete='cascade'), nullable=false) user = relationship("user", backref=backref('comments', cascade="all,delete", order_by=id))
this kind of setup works me, user
class doesn't have special fields except primary key. basically, works intended, when delete user - comments gone well. doesn't have deletion through sqlalchemy
either, code creates appropriate table structure, if delete user manually (sql query) - cascade deletion still work.
Comments
Post a Comment