I have two tables, activities and campaigns (yes, it's for a internal-custom CRM application, kind of):
create table `tb_activities` (
`organization_id` integer(20) unsigned not null default '0',
`activity_id` integer(20) unsigned not null auto_increment,
primary key (`activity_id`)
) engine=innodb default charset=utf8 pack_keys=1 row_format=compact;
create table `tb_campaigns` (
`organization_id` integer(20) unsigned not null default '0',
`campaign_id` integer(20) unsigned not null auto_increment,
primary key (`campaign_id`)
) engine=innodb default charset=utf8 pack_keys=1 row_format=compact;
And in between a third many-to-many table with foreign keys to both tables.
drop table if exists `tb_campaigns_activities`;
create table `tb_campaigns_activities` (
`organization_id` integer(20) unsigned not null default '0',
`campaign_id` integer(20) unsigned not null default '0',
`activity_id` integer(20) unsigned not null default '0',
primary key (`organization_id`,`campaign_id`,`activity_id`),
foreign key `rc_campaigns_activities_a` (`organization_id`,`campaign_id`)
references `tb_campaigns` (`organization_id`,`campaign_id`)
on update restrict on delete restrict,
foreign key `rc_campaigns_activities_b` (`organization_id`,`activity_id`)
references `tb_activities` (`organization_id`,`activity_id`)
on update restrict on delete restrict,
key `dc_campaigns_activities_a` (`organization_id`,`campaign_id`),
key `dc_campaigns_activities_b` (`organization_id`,`activity_id`)
) engine=innodb default charset=utf8 pack_keys=1 row_format=compact;
But when I try to install it, I get a 105 mysql error. As far I've researched this error is about bad bad-formed foreign key, and the only way I've got it to work was to remove both dc_campaigns_activities_a and dc_campaigns_activities_b relations. So the database is complaining about a logical error, but am I crazy or what? To me it's a perfect foreign key scenario; the only thing it that organization_id is shared by both keys. Why this isn't correct?
You can only add a foreign key that is a foreign key on the referenced table. (organization_id,campaign_id)
is not a key on tb_campaigns
(same with activities). The column types must also match, but they do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.