i have trouble with a mysql ERM that is presently driving me crazy. I've got a table known as usuaris_backoffice accustomed to store backoffice customers, i quickly in addition have a table known as usuaris_backoffice_permisos accustomed to keep rights of each and every user. I in addition have a table known as perfils where i store some profiles which may be edited through the backoffice users so to possess a edition history there's a tabled known as perfils_usuari_backoffice where i store which profile continues to be modified by each user. I quickly in addition have a tabled known as widgets which is also edited through the customers, therefore the icons edition history is saved in widgets_usuari_backoffice. The FK are enabled for cascade deletion, then when a person is erased from usuaris_backoffice its rights will also be erased from usuaris_backoffice_permisos. The issue seems after i attempt to:

 truncate usuaris_backoffice_permisos

which provides me the next error:

 1701 - Cannot truncate a table referenced in a foreign key constraint (`test`.`usuaris_backoffice_permisos`, CONSTRAINT `FK_F8F850F3D001730C` FOREIGN KEY (`usuari_backoffice_id`) REFERENCES `test`.`usuaris_backoffice` (`id`)) 

Because this 6 tables are members of a greatest ERM i have produced a little test atmosphere to be able to have the ability to run some tests, therefore the following code may be used to create and populate the tables:


CREATE TABLE perfils_usuari_backoffice (perfil_id INT NOT NULL, login VARCHAR(20) NOT NULL, up-to-date_at DATETIME NOT NULL, INDEX IDX_870785D657291544 (perfil_id), INDEX IDX_870785D6AA08CB10 (login), PRIMARY KEY(perfil_id, login, up-to-date_at)) ENGINE = InnoDB

CREATE TABLE usuaris_backoffice (id INT AUTO_INCREMENT NOT NULL, login VARCHAR(20) NOT NULL, password VARCHAR(32) NOT NULL, produced_at DATETIME NOT NULL, up-to-date_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, produced_at DATETIME NOT NULL, up-to-date_at DATETIME NOT NULL, INDEX IDX_F8F850F3D001730C (usuari_backoffice_id), PRIMARY KEY(id)) ENGINE = InnoDB

CREATE TABLE icons_usuari_backoffice (widget_id INT NOT NULL, usuari_backoffice_id INT NOT NULL, up-to-date_at DATETIME NOT NULL, INDEX IDX_43DA3B33FBE885E2 (widget_id), INDEX IDX_43DA3B33D001730C (usuari_backoffice_id), PRIMARY KEY(widget_id, usuari_backoffice_id, up-to-date_at)) ENGINE = InnoDB

CREATE TABLE icons (id INT AUTO_INCREMENT NOT NULL, classe VARCHAR(20) NOT NULL, up-to-date_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, produced_at DATETIME NOT NULL, up-to-date_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 Remove CASCADE

ALTER TABLE usuaris_backoffice_permisos ADD CONSTRAINT FK_F8F850F3D001730C FOREIGN KEY (usuari_backoffice_id) REFERENCES usuaris_backoffice (id) ON Remove CASCADE

ALTER TABLE perfils_usuari_backoffice ADD CONSTRAINT FK_870785D6AA08CB10 FOREIGN KEY (login) REFERENCES usuaris_backoffice (login) ON Remove CASCADE

ALTER TABLE icons_usuari_backoffice ADD CONSTRAINT FK_43DA3B33D001730C FOREIGN KEY (usuari_backoffice_id) REFERENCES usuaris_backoffice (id) ON Remove CASCADE

ALTER TABLE icons_usuari_backoffice ADD CONSTRAINT FK_43DA3B33FBE885E2 FOREIGN KEY (widget_id) REFERENCES icons (id) ON Remove CASCADE

Place 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')

Place 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')

Place INTO `test`.`perfils` (`id`, `actiu`, `created_at`, `updated_at`) VALUES (NULL, '0', '2011-10-06 00:00:00', '2011-10-06 00:00:00')

Place INTO `test`.`perfils_usuari_backoffice` (`perfil_id`, `login`, `updated_at`) VALUES ('1', 'edgar', '2011-10-06 00:00:00')

Place INTO `test`.`widgets` (`id`, `classe`, `updated_at`) VALUES (NULL, 'hola', '2011-10-06 00:00:00')

Place 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 mistake is:

TRUNCATE usuaris_backoffice_permisos;
TRUNCATE perfils_usuari_backoffice;
TRUNCATE widgets_usuari_backoffice;
TRUNCATE usuaris_backoffice;

Thanks for the time, hope someone might help :)