在MySQL中创建外部约束
在我经常使用的情况下(使用Play Framework!)使用MySQL安装应用程序时,我遇到了一个错误 应用SQL时,我遇到以下错误:在MySQL中创建外部约束,mysql,sql,playframework,Mysql,Sql,Playframework,在我经常使用的情况下(使用Play Framework!)使用MySQL安装应用程序时,我遇到了一个错误 应用SQL时,我遇到以下错误: 160909 16:03:26 Error in foreign key constraint of table knockadoonserver/action: there is no index in the table which would contain the columns as the first columns, or the data ty
160909 16:03:26 Error in foreign key constraint of table knockadoonserver/action:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT "fk_action_switch_id" FOREIGN KEY ("switch_id") REFERENCES "switch" ("id")
但这是为什么呢?我的数据库设置是否错误(这是一个新安装),因为我已经多次这样做(在其他应用程序中)
完整的SQL是:
create table switch (
id bigint auto_increment not null,
name varchar(255),
description varchar(255),
gpio integer,
turned_on BOOLEAN,
device_id bigint,
last_update datetime not null,
add_date datetime not null,
constraint pk_switch primary key (id)
);
create table sensor (
id bigint auto_increment not null,
serial varchar(255),
name varchar(255),
value float,
main_sensor BOOLEAN,
device_id bigint,
last_update datetime not null,
add_date datetime not null,
constraint pk_sensor primary key (id)
);
create table action (
id bigint auto_increment not null,
message varchar(255),
name varchar(255),
type integer,
start_time datetime,
end_time datetime,
daily BOOLEAN,
min_value FLOAT,
max_value FLOAT,
disabled BOOLEAN,
sensor_id bigint,
switch_id bigint,
last_update datetime not null,
add_date datetime not null,
constraint pk_action primary key (id)
);
alter table switch add constraint fk_switch_device_id foreign key (device_id) references device (id) on delete restrict on update restrict;
create index ix_switch_device_id on switch (device_id);
alter table sensor add constraint fk_sensor_device_id foreign key (device_id) references device (id) on delete restrict on update restrict;
create index ix_sensor_device_id on sensor (device_id);
alter table action add constraint fk_action_switch_id foreign key (switch_id) references switch (id) on delete restrict on update restrict;
create index ix_action_switch_id on action (switch_id);
alter table action add constraint fk_action_sensor_id foreign key (sensor_id) references sensor (id) on delete restrict on update restrict;
create index ix_action_sensor_id on action (sensor_id);
我留在设备上的foreign contains中,因为这是一个已经创建的表,似乎没有问题(我猜是之前的问题)。SQL是:
create table device (
id bigint auto_increment not null,
name varchar(255),
hostname varchar(255),
ip_address varchar(255),
last_connection datetime,
on_alarm boolean,
on_silent boolean,
alarm_whole_camp boolean,
map_xloc smallint,
map_yloc smallint,
last_update datetime not null,
add_date datetime not null,
constraint pk_device primary key (id)
);
一定是小东西,但我现在已经花了好几个小时在这上面了
上面的SQL看起来还可以,所以它一定是如何应用的。我发现了这个错误:
[编辑:]
“./database/#sql-47f_8e”到“./database/action”(错误号:150)
[错误:1025,SQLSTATE:HY000]
开头引用的错误是“ShowEngine INNODB STATUS;”错误的结果
[编辑2:]
| action | CREATE TABLE `action` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`message` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`type` int(11) DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`daily` tinyint(1) DEFAULT NULL,
`min_value` float DEFAULT NULL,
`max_value` float DEFAULT NULL,
`disabled` tinyint(1) DEFAULT NULL,
`sensor_id` bigint(20) DEFAULT NULL,
`switch_id` bigint(20) DEFAULT NULL,
`last_update` datetime NOT NULL,
`add_date` datetime NOT NULL,
`override_time` datetime DEFAULT NULL,
`overrule_temp` tinyint(1) DEFAULT NULL,
`overrule_time` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ix_action_switch_id` (`switch_id`),
KEY `ix_action_sensor_id` (`sensor_id`),
CONSTRAINT `fk_action_sensor_id` FOREIGN KEY (`sensor_id`) REFERENCES `sensor` (`id`),
CONSTRAINT `fk_action_switch_id` FOREIGN KEY (`switch_id`) REFERENCES `switch` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
使这两列不为空?这在我的5.6.31上很好,也许可以使这两列不为空?有时没有与操作相关的开关或传感器,所以有时它们为空。我只是一个接一个地运行我的所有查询(而不是让它们同时应用),它工作正常。因此,框架应用它们的过程中一定有一些东西。我添加了一些信息。这一定与如何应用一切有关。
| action | CREATE TABLE `action` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`message` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`type` int(11) DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`daily` tinyint(1) DEFAULT NULL,
`min_value` float DEFAULT NULL,
`max_value` float DEFAULT NULL,
`disabled` tinyint(1) DEFAULT NULL,
`sensor_id` bigint(20) DEFAULT NULL,
`switch_id` bigint(20) DEFAULT NULL,
`last_update` datetime NOT NULL,
`add_date` datetime NOT NULL,
`override_time` datetime DEFAULT NULL,
`overrule_temp` tinyint(1) DEFAULT NULL,
`overrule_time` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ix_action_switch_id` (`switch_id`),
KEY `ix_action_sensor_id` (`sensor_id`),
CONSTRAINT `fk_action_sensor_id` FOREIGN KEY (`sensor_id`) REFERENCES `sensor` (`id`),
CONSTRAINT `fk_action_switch_id` FOREIGN KEY (`switch_id`) REFERENCES `switch` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
create table action (
id bigint auto_increment not null,
message varchar(255),
name varchar(255),
type integer,
start_time datetime,
end_time datetime,
daily BOOLEAN,
min_value FLOAT,
max_value FLOAT,
disabled BOOLEAN,
sensor_id bigint NOT NULL,
switch_id bigint NOT NULL,
last_update datetime not null,
add_date datetime not null,
constraint pk_action primary key (id)
)ENGINE=INNODB;