mysql - Error #1215 - Cannot add foreign key constraint -
i have generated sql script mysql workbench.
when try create database have "error 1215 1215 - cannot add foreign key constraint".
the problem seems last foreign key in last create table :
constraint `fk_tb_animation_option_col_debut` foreign key (`dt_ano_debut`) references `brest2016`.`tb_animation_option` (`dt_ano_debut`) on delete no action on update no action)
this entire script:
-- mysql script generated mysql workbench -- 01/16/16 00:46:22 -- model: new model version: 1.0 -- mysql workbench forward engineering set @old_unique_checks=@@unique_checks, unique_checks=0; set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0; set @old_sql_mode=@@sql_mode, sql_mode='traditional,allow_invalid_dates'; -- ----------------------------------------------------- -- schema brest2016 -- ----------------------------------------------------- create schema if not exists `brest2016` default character set utf8 ; use `brest2016` ; -- ----------------------------------------------------- -- table `brest2016`.`tb_visiteur` -- ----------------------------------------------------- create table if not exists `brest2016`.`tb_visiteur` ( `d_vi_code` int(3) not null auto_increment, `v_vi_nom` varchar(45) not null, `v_vi_prenom` varchar(45) not null, `v_vi_email` varchar(45) not null, `v_vi_telephone` varchar(45) null, `dt_vi_debut` datetime not null, `dt_vi_fin` datetime not null, primary key (`d_vi_code`), unique index `idvisiteur_unique` (`d_vi_code` asc), unique index `email_unique` (`v_vi_email` asc)) engine = innodb; -- ----------------------------------------------------- -- table `brest2016`.`tb_type_animation` -- ----------------------------------------------------- create table if not exists `brest2016`.`tb_type_animation` ( `d_ty_code` int(3) not null auto_increment, `v_ty_libelle` varchar(45) null, primary key (`d_ty_code`)) engine = innodb; -- ----------------------------------------------------- -- table `brest2016`.`tb_animation` -- ----------------------------------------------------- create table if not exists `brest2016`.`tb_animation` ( `d_an_code` int(3) not null auto_increment, `v_an_libelle` varchar(45) not null, `v_an_lieu` varchar(45) not null, `v_an_image` varchar(45) null, `v_an_descriptif` varchar(45) null, `d_an_priorite` varchar(45) not null, `d_ty_code` int(3) null, primary key (`d_an_code`), unique index `idanimation_unique` (`d_an_code` asc), index `fk_tb_type_animation_col_code_1_idx` (`d_ty_code` asc), constraint `fk_tb_type_animation_col_code` foreign key (`d_ty_code`) references `brest2016`.`tb_type_animation` (`d_ty_code`) on delete no action on update no action) engine = innodb; -- ----------------------------------------------------- -- table `brest2016`.`tb_option` -- ----------------------------------------------------- create table if not exists `brest2016`.`tb_option` ( `d_op_code` int(3) not null auto_increment, `v_op_libelle` varchar(45) not null, primary key (`d_op_code`), unique index `idoption_unique` (`d_op_code` asc)) engine = innodb; -- ----------------------------------------------------- -- table `brest2016`.`tb_animation_option` -- ----------------------------------------------------- create table if not exists `brest2016`.`tb_animation_option` ( `d_an_code` int(3) not null, `d_op_code` int(3) not null, `dt_ano_debut` datetime not null, `dt_ano_fin` datetime not null, `d_ano_places_max` int(3) not null, `d_ano_duree` int(3) not null, primary key (`d_an_code`, `d_op_code`, `dt_ano_debut`), index `fk_tb_option_idx` (`d_op_code` asc), constraint `fk_tb_animation_col_code` foreign key (`d_an_code`) references `brest2016`.`tb_animation` (`d_an_code`) on delete no action on update no action, constraint `fk_tb_option_col_code` foreign key (`d_op_code`) references `brest2016`.`tb_option` (`d_op_code`) on delete no action on update no action) engine = innodb comment = 'association entre une animation et une option'; -- ----------------------------------------------------- -- table `brest2016`.`tb_administrateur` -- ----------------------------------------------------- create table if not exists `brest2016`.`tb_administrateur` ( `d_ad_code` int(3) not null, `v_ad_id` varchar(12) not null, `v_ad_mdp` varchar(12) not null, primary key (`d_ad_code`), unique index `idadministrateur_unique` (`d_ad_code` asc), unique index `v_ut_login_unique` (`v_ad_id` asc)) engine = innodb; -- ----------------------------------------------------- -- table `brest2016`.`tb_reservation` -- ----------------------------------------------------- create table if not exists `brest2016`.`tb_reservation` ( `d_vi_code` int(3) not null, `d_an_code` int(3) not null, `d_op_code` int(3) not null, `dt_ano_debut` datetime not null, primary key (`d_vi_code`, `d_an_code`, `d_op_code`, `dt_ano_debut`), index `fk_tb_animation_option_col_code_animation_idx` (`d_an_code` asc), index `fk_tb_animation_option_col_code_option_idx` (`d_op_code` asc), index `fk_tb_animation_option_col_debut_idx` (`dt_ano_debut` asc), constraint `fk_tb_visiteur_col_code` foreign key (`d_vi_code`) references `brest2016`.`tb_visiteur` (`d_vi_code`) on delete no action on update no action, constraint `fk_tb_animation_option_col_code_animation` foreign key (`d_an_code`) references `brest2016`.`tb_animation_option` (`d_an_code`) on delete no action on update no action, constraint `fk_tb_animation_option_col_code_option` foreign key (`d_op_code`) references `brest2016`.`tb_animation_option` (`d_op_code`) on delete no action on update no action, constraint `fk_tb_animation_option_col_debut` foreign key (`dt_ano_debut`) references `brest2016`.`tb_animation_option` (`dt_ano_debut`) on delete no action on update no action) engine = innodb; set sql_mode=@old_sql_mode; set foreign_key_checks=@old_foreign_key_checks; set unique_checks=@old_unique_checks;
the column named in foreign key must indexed.
create table if not exists `brest2016`.`tb_animation_option` ( `d_an_code` int(3) not null, `d_op_code` int(3) not null, `dt_ano_debut` datetime not null, `dt_ano_fin` datetime not null, `d_ano_places_max` int(3) not null, `d_ano_duree` int(3) not null, primary key (`d_an_code`, `d_op_code`, `dt_ano_debut`), index `fk_tb_option_idx` (`d_op_code` asc), index `dt_ano_debut_idx` (`t_ano_debut`), -- add line constraint `fk_tb_animation_col_code` foreign key (`d_an_code`) references `brest2016`.`tb_animation` (`d_an_code`) on delete no action on update no action, constraint `fk_tb_option_col_code` foreign key (`d_op_code`) references `brest2016`.`tb_option` (`d_op_code`) on delete no action on update no action) engine = innodb comment = 'association entre une animation et une option';
Comments
Post a Comment