【问题标题】:Creating a foreign constraint in MySQL在 MySQL 中创建外部约束
【发布时间】:2016-09-09 15:19:55
【问题描述】:

在我经常使用的情况下(使用 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 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' (errno: 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 |

【问题讨论】:

  • 在我的 5.6.31 上运行良好
  • 也许让这两列不为空?
  • 有时没有与动作关联的开关或传感器,因此有时它们为空。
  • 我只是一个一个地运行我的所有查询(而不是同时应用它们)并且它有效。所以框架应用它们的过程中一定有一些东西。
  • 我添加了一些信息。它必须与如何应用所有内容有关。

标签: mysql sql playframework


【解决方案1】:
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;

让这两列不为空?

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2012-09-07
    • 2014-03-24
    相关资源
    最近更新 更多