MySQL Cannot Add Foreign Key Constraint

后端 未结 22 1855
时光取名叫无心
时光取名叫无心 2020-11-22 08:35

So I\'m trying to add Foreign Key constraints to my database as a project requirement and it worked the first time or two on different tables, but I have two tables on which

22条回答
  •  悲&欢浪女
    2020-11-22 08:59

    I had a similar error in creating foreign key in a Many to Many table where the primary key consisted of 2 foreign keys and another normal column. I fixed the issue by correcting the referenced table name i.e. company, as shown in the corrected code below:

    create table company_life_cycle__history -- (M-M)
    (
    company_life_cycle_id tinyint unsigned not null,
    Foreign Key (company_life_cycle_id) references company_life_cycle(id) ON DELETE    CASCADE ON UPDATE CASCADE,
    company_id MEDIUMINT unsigned not null,
    Foreign Key (company_id) references company(id) ON DELETE CASCADE ON UPDATE CASCADE,
    activity_on date NOT NULL,
    PRIMARY KEY pk_company_life_cycle_history (company_life_cycle_id, company_id,activity_on),
    created_on datetime DEFAULT NULL,
    updated_on datetime DEFAULT NULL,
    created_by varchar(50) DEFAULT NULL,
    updated_by varchar(50) DEFAULT NULL
    );
    

提交回复
热议问题