Foreign Key Checks During Migrations

I’ve had a good search around and could not find an answer to this, but apologies if it’s been shared elsewhere.

I’m going to create and run quite a large migration on an existing database to move it over to a new system and I’d like to disable foreign key checks during it.

Because the migration will be making a fair few changes I’ll be using safeUp and safeDown.

So my question is, do I need to disable foreign key checks at the top of the migration and enable again at the bottom? safeUp and safeDown don’t do anything clever with foreign key checks already do they?

and if I do need to, what’s the best way to do that? There is no built in function similar to $this->addColumn(); so build an sql query and execute it?

Thanks.

To answer my own question I did need to ignore foreign key constrains so I did…


$this->execute("SET foreign_key_checks = 0;");

…at the top and then…


$this->execute("SET foreign_key_checks = 1;");

…at the bottom of the migration.

2 Likes