MySQL: error on truncate `myTable` when FK has on Delete Cascade enabled
i have a problem with a mysql ERM which is currently driving me crazy. I have a table called usuaris_backoffice
used to store backoffice users, then i also have a table called usuaris_backoffice_permisos
used to store the privileges of every user. I also have a table called perfils
where i store some profiles which can be edited by the backoffice users
so to have a edition history there is a tabled called perfils_usuari_backoffice
where i store which profile
has been modified by each user
. Then i also have a tabled called widgets
which also can be edited by the users, so the widgets edition history is stored in widgets_usuari_backoffice
. The FK are enabled for cascade deletion, so when a user is deleted from usuaris_backoffice
its privileges are also deleted from usuaris_backoffice_permisos
. The problem appears when i try to:
truncate usuaris_backoffice_permisos
which gives me the following error:
1701 - Cannot truncate a tabl开发者_开发知识库e referenced in a foreign key constraint (`test`.`usuaris_backoffice_permisos`, CONSTRAINT `FK_F8F850F3D001730C` FOREIGN KEY (`usuari_backoffice_id`) REFERENCES `test`.`usuaris_backoffice` (`id`))
As this 6 tables are part of a biggest ERM i've created a small test environment in order to be able to run some tests, so the following code can be used to create
and populate
the tables
:
CREATE TABLE perfils_usuari_backoffice (perfil_id INT NOT NULL, login VARCHAR(20) NOT NULL, updated_at DATETIME NOT NULL, INDEX IDX_870785D657291544 (perfil_id), INDEX IDX_870785D6AA08CB10 (login), PRIMARY KEY(perfil_id, login, updated_at)) ENGINE = InnoDB; CREATE TABLE usuaris_backoffice (id INT AUTO_INCREMENT NOT NULL, login VARCHAR(20) NOT NULL, password VARCHAR(32) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, UNIQUE INDEX UNIQ_95E4B6E5AA08CB10 (login), PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE usuaris_backoffice_permisos (id INT AUTO_INCREMENT NOT NULL, usuari_backoffice_id INT NOT NULL, es_admin TINYINT(1) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX IDX_F8F850F3D001730C (usuari_backoffice_id), PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE widgets_usuari_backoffice (widget_id INT NOT NULL, usuari_backoffice_id INT NOT NULL, updated_at DATETIME NOT NULL, INDEX IDX_43DA3B33FBE885E2 (widget_id), INDEX IDX_43DA3B33D001730C (usuari_backoffice_id), PRIMARY KEY(widget_id, usuari_backoffice_id, updated_at)) ENGINE = InnoDB; CREATE TABLE widgets (id INT AUTO_INCREMENT NOT NULL, classe VARCHAR(20) NOT NULL, updated_at DATETIME NOT NULL, UNIQUE INDEX UNIQ_9D58E4C18F87BF96 (classe), PRIMARY KEY(id)) ENGINE = InnoDB; CREATE TABLE perfils (id INT AUTO_INCREMENT NOT NULL, actiu TINYINT(1) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB; ALTER TABLE perfils_usuari_backoffice ADD CONSTRAINT FK_870785D657291544 FOREIGN KEY (perfil_id) REFERENCES perfils (id) ON DELETE CASCADE; ALTER TABLE usuaris_backoffice_permisos ADD CONSTRAINT FK_F8F850F3D001730C FOREIGN KEY (usuari_backoffice_id) REFERENCES usuaris_backoffice (id) ON DELETE CASCADE; ALTER TABLE perfils_usuari_backoffice ADD CONSTRAINT FK_870785D6AA08CB10 FOREIGN KEY (login) REFERENCES usuaris_backoffice (login) ON DELETE CASCADE; ALTER TABLE widgets_usuari_backoffice ADD CONSTRAINT FK_43DA3B33D001730C FOREIGN KEY (usuari_backoffice_id) REFERENCES usuaris_backoffice (id) ON DELETE CASCADE; ALTER TABLE widgets_usuari_backoffice ADD CONSTRAINT FK_43DA3B33FBE885E2 FOREIGN KEY (widget_id) REFERENCES widgets (id) ON DELETE CASCADE; INSERT INTO `test`.`usuaris_backoffice` (`id` ,`login` ,`password` ,`created_at` ,`updated_at`)VALUES (NULL , 'edgar', '1234', '2011-10-06 00:00:00', '2011-10-06 00:00:00'); INSERT INTO `test`.`usuaris_backoffice_permisos` (`id`, `usuari_backoffice_id`, `es_admin`, `created_at`, `updated_at`) VALUES (NULL, '1', '0', '2011-10-06 00:00:00', '2011-10-06 00:00:00'); INSERT INTO `test`.`perfils` (`id`, `actiu`, `created_at`, `updated_at`) VALUES (NULL, '0', '2011-10-06 00:00:00', '2011-10-06 00:00:00'); INSERT INTO `test`.`perfils_usuari_backoffice` (`perfil_id`, `login`, `updated_at`) VALUES ('1', 'edgar', '2011-10-06 00:00:00'); INSERT INTO `test`.`widgets` (`id`, `classe`, `updated_at`) VALUES (NULL, 'hola', '2011-10-06 00:00:00'); INSERT INTO `test`.`widgets_usuari_backoffice` (`widget_id`, `usuari_backoffice_id`, `updated_at`) VALUES ('1', '1', '2011-10-06 00:00:00');
Code which returns the error is:
TRUNCATE usuaris_backoffice_permisos;
TRUNCATE perfils_usuari_backoffice;
TRUNCATE widgets_usuari_backoffice;
TRUNCATE usuaris_backoffice;
Thanks a lot for your time, hope someone can help :)
Take a look http://dev.mysql.com/doc/refman/5.5/en/truncate-table.html. It says
TRUNCATE TABLE fails for an InnoDB table if there are any FOREIGN KEY constraints from other tables that reference the table. Foreign key constraints between columns of the same table are allowed.
From here it looks it worked in some previous versions
SET FOREIGN_KEY_CHECKS = 0;
Then truncate your tables.
精彩评论