0

我有两个表格,活动和活动(是的,它是用于内部自定义 CRM 应用程序的):

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;

在第三个多对多表之间,两个表都有外键。

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;

但是当我尝试安装它时,我得到一个 105 mysql 错误。据我研究,此错误与格式错误的外键有关,我让它工作的唯一方法是删除 dc_campaigns_activities_a 和 dc_campaigns_activities_b 关系。所以数据库抱怨逻辑错误,但我疯了还是什么?对我来说,这是一个完美的外键场景;唯一的事情是 organization_id 由两个键共享。为什么这是不正确的?

4

1 回答 1

2

您只能添加一个外键,它是被引用表上的外键。 (organization_id,campaign_id)不是一个关键tb_campaigns(与活动相同)。列类型也必须匹配,但它们确实匹配。

于 2013-06-24T23:22:36.607 回答