在MySQL中创建外部约束

时间:2016-09-09 15:19:55

标签: mysql sql playframework

在我经常使用的情况下使用MySQL安装应用程序时遇到错误(使用Play Framework!)。

应用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 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);

我留在设备上的外部包含,因为这是一个已经创建的表,并且看起来不是问题(我猜它是先前的)。 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似乎没问题,所以它必须与它的应用方式有关。我发现了这个错误:

[编辑:]

2016-09-09 16:03:26,823 [ERROR] from play.api.db.evolutions.DefaultEvolutionsApi in main - Error on rename
     ' ./ database / #sql-47f_8e'到' ./ database / action' (错误:150)   [错误:1025,SQLSTATE:HY000]

开头引用的错误是" SHOW ENGINE 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 |

1 个答案:

答案 0 :(得分:0)

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;

使这两列不为空?