mysql - Odd Error in mariaDB Foreign Keys -


hi hope 1 can problem when try add foreign key constraint error.

my database name "hazard"

child:

create table `child` (     `id` int(11) not null auto_increment,     `a` int(11) null default null,     `b` int(11) null default null,     primary key (`id`) ) collate='utf8_general_ci' 

parent:

create table `parent` (     `id` int(11) not null auto_increment,     `alfa` int(11) null default null,     `beta` int(11) null default null,     primary key (`id`) ) collate='utf8_general_ci' 

those create codes (using heidisql)

and when try add foreign key with

alter table child modify column int,add constraint fk_parent_child foreign key(a) references parent(alfa); 

or

alter table child add constraint fk1 foreign key (a) references parent(alfa); 

i same error

can't create table 'hazard.#sql-d04_53' (errno: 150)

this happening many of classmates using mariadb , mysql

beforehand apology inconvenience , hope guys can us.

error 150 means updating tables in wrong order. is, first insert violates foreign key constraint second insert fix.

in case doing alter instead of insert. swap order of alters. if not work, check data see won't violating fk constraints. if past that, read on...

in extreme cases, can turn off foreign key constraints while doing inserts, turn them on. (but leaves vulnerable screw-ups.)


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -