在新服务器上创建数据库后,外键约束失败

时间:2018-08-31 00:51:20

标签: mysql sql innodb

我正在尝试插入一条记录,但出现“外键约束失败”错误。我一直在搜索,发现每次出现此问题都是因为它们要添加的记录的主键在所引用的表中不存在。在这种情况下,我看到引用表中存在主键,但仍无法插入记录。

存在一个model_id为171的模型,但是当尝试添加具有该id的model_feature记录时,它失败了:

mysql> select distinct model_id from model;
+----------+
| model_id |
+----------+
|      167 |
|      168 |
|      169 |
|      170 |
|      171 |
+----------+
5 rows in set (0.00 sec)

mysql> INSERT INTO model_feature (feature_position, model_id, feature_name)  VALUES (0, 171, 'my_feature');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`bpp`.`model_feature`, CONSTRAINT `model_feature_ibfk_1` FOREIGN KEY (`model_id`) REFERENCES `Model` (`model_id`) ON DELETE CASCADE)

以下是model_feature的详细信息:

mysql> show create table model_feature;
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| model_feature | CREATE TABLE `model_feature` (
  `model_id` int(11) DEFAULT NULL,
  `feature_name` varchar(25) DEFAULT NULL,
  `feature_position` int(11) DEFAULT NULL,
  `model_feature_id` int(11) NOT NULL AUTO_INCREMENT,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`model_feature_id`),
  KEY `model_id` (`model_id`),
  CONSTRAINT `model_feature_ibfk_1` FOREIGN KEY (`model_id`) REFERENCES `Model` (`model_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1343 DEFAULT CHARSET=utf8 |
+---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show columns from model_feature;
+------------------+-------------+------+-----+-------------------+-----------------------------+
| Field            | Type        | Null | Key | Default           | Extra                       |
+------------------+-------------+------+-----+-------------------+-----------------------------+
| model_id         | int(11)     | YES  | MUL | NULL              |                             |
| feature_name     | varchar(25) | YES  |     | NULL              |                             |
| feature_position | int(11)     | YES  |     | NULL              |                             |
| model_feature_id | int(11)     | NO   | PRI | NULL              | auto_increment              |
| timestamp        | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------------+-------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.00 sec)

请注意,我的Mac上有此数据库,最近我通过mysqldump-upping数据,将其scp-到我的实例,将其加载并删除记录的方式将模式复制到了AWS(我想这里有一种更好的方法,但这似乎很容易)。这是在AWS实例上失败的。相同的插件在Mac / OSX上也可以正常工作。

关于为什么失败的任何建议?

0 个答案:

没有答案