Pav Sidhu
Pav Sidhu

Reputation: 6954

SQLAlchemy ondelete and onupdate do not update the database

I'm using Flask-SQLAlchemy with a MySQL database using the InnoDB engine which does support ondelete and onupdate.

I have the following in my 'Posts' table which refers to an Accounts table:

account_id = db.Column(db.Integer, db.ForeignKey('accounts.id', onupdate="CASCADE", ondelete="CASCADE"))

I updated the database using Flask-Migrate (Alembic) and I've noticed that the onupdate and ondelete values are not changed when I specify onupdate="CASCADE", ondelete="CASCADE" even though it states I should use it like this in the SQLAlchemy Documentation

What could be the issue? Thanks.

Upvotes: 3

Views: 2227

Answers (1)

Pav Sidhu
Pav Sidhu

Reputation: 6954

The reason the ondelete and onupdate values don't update is because they cannot be changed when there is already that foreign key present.

To change it, I had to remove the foreign key from the table, update the database and then re-add it to the table with the values you want then update once again.

Upvotes: 3

Related Questions