Cascading SQLAlchemy in many ways

I am trying to cascade delete in my database using SQLAlchemy. I have tables (with fields removed):

action_permissions_to_groups = db.Table('action_permissions_to_groups', 
    db.Column('group_id', db.Integer, db.ForeignKey('group.id',  ondelete='cascade')),
    db.Column('action_permission_id', db.Integer, db.ForeignKey('action_permission.id',  ondelete='cascade'))
)

class Group(db.Model, MyModel):
    id = db.Column(db.Integer, primary_key=True)
    action_permissions = db.relationship('ActionPermission', secondary=action_permissions_to_groups, cascade='delete')


class ActionPermission(db.Model, MyModel):
    id = db.Column(db.Integer, primary_key=True)

In English, several Groupshave several ActionPermissions. When I delete Group, I want everything in the table to be deleted action_permissions_to_groupsfrom all rows where it group_idcorresponds to the group to be deleted.

The above code gives me the following error when trying to delete a group:

InvalidRequestError: This session transaction was canceled due to a previous exception during a flash. To start a new transaction from this session, first issue Session.rollback (). The original exception was: the DELETE statement in the "action_permissions_to_groups" table must delete 11 rows; Only 12 were matched.

- , ?

!!

+4

All Articles