错误:
| Error 2013-08-08 08:45:14,624 [main] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table student_knowledge_mappings add index FKCA7A648A86D99F36 (updated_by_id), add constraint FKCA7A648A86D99F36 foreign key (updated_by_id) references user (id)
| Error 2013-08-08 08:45:14,626 [main] ERROR hbm2ddl.SchemaUpdate - Cannot add or update a child row: a foreign key constraint fails (`SLA`.<result 2 when explaining filename '#sql-1a7c_4'>, CONSTRAINT `FKCA7A648A86D99F36` FOREIGN KEY (`updated_by_id`) REFERENCES `user` (`id`))
| Error 2013-08-08 08:45:14,671 [main] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table topic_levels_cc add index FKCE4A204F17E447 (topic_id), add constraint FKCE4A204F17E447 foreign key (topic_id) references topic_cc (id)
| Error 2013-08-08 08:45:14,671 [main] ERROR hbm2ddl.SchemaUpdate - Can't create table 'SLA.#sql-1a7c_4' (errno: 150)
答案 0 :(得分:1)
这通常是由于表结构上设置的mysql引擎类型不一致而发生的:
driverClassName ="com.mysql.jdbc.Driver"
username = "username"
password = "password"
zeroDateTimeBehavior="convertToNull"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
如果您使用的是mysql,首先要检查的是确保您的方言,并确保将其设置为如上所述。
现在这意味着你的所有表实际上应该是InnoDB ..
要做到这一点,请将PASSWORD和DATABASE_NAME更改为您的详细信息:
mysql -u root -pPASSWORD DATABASE_NAME -e "show table status where Engine='MyISAM';" | awk 'NR>1 {print "ALTER TABLE "$1" ENGINE = InnoDB;"}' | mysql -u root -pPASSWORD DATABASE_NAME
或者登录到mysql并执行它然后使用输出泵将其重新导入mysql:
SET @DATABASE_NAME = 'DATABASE_NAME';
SELECT CONCAT('ALTER TABLE ', table_name, ' ENGINE=InnoDB;') AS sql_statements
FROM information_schema.tables AS tb
WHERE table_schema = @DATABASE_NAME
AND `ENGINE` = 'MyISAM'
AND `TABLE_TYPE` = 'BASE TABLE'
ORDER BY table_name DESC INTO OUTFILE '/tmp/myoutput.sql';
exit;
现在来自Linux cli:
mysql DATABASE_NAME -u root -p < /tmp/myoutput.sql
您也可以将我的默认值定义为InnoDB
mysql> SET SESSION storage_engine = InnoDB;
mysql> SET GLOBAL storage_engine = InnoDB;
但实际上并没有任何需要,因为它将从那里正确存储所有内容。
如果没有定义方言,它将默认为mysql的默认配置,这将是MyISAM,然后您可以将上述所有内容反转为MyISAM而不是InnoDB
更新---- 2015年2月
虽然尝试升级我遇到了同样的老问题,但这次解决方案略有不同:
alter table servers_links add constraint FK_oder1rqc5maeen9dlb9sbpoav foreign key (servers_links_id) references servers (id)
2015-02-09 14:49:12,954 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Cannot add or update a child row: a foreign key constraint fails (`myDB_prod`.<result 2 when explaining filename '#sql-736_d6'>, CONSTRAINT `FK_oder1rqc5maeen9dlb9sbpoav` FOREIGN KEY (`servers_links_id`) REFERENCES `servers` (`id`))
所以我们要跟踪 servers_links
中的条目select * from servers_links limit 2;
+------------------+----------+
| servers_links_id | links_id |
+------------------+----------+
| 1 | 3 |
select a.servers_links_id, b.name FROM servers_links a LEFT JOIN servers b on a.servers_links_id=b.id where b.name is NULL;
+------------------+------+
| servers_links_id | name |
+------------------+------+
| 89 | NULL |
| 89 | NULL |
| 89 | NULL |
| 91 | NULL |
| 91 | NULL |
| 91 | NULL |
| 93 | NULL |
| 93 | NULL |
| 93 | NULL |
| 95 | NULL |
| 95 | NULL |
| 95 | NULL |
| 111 | NULL |
| 111 | NULL |
| 112 | NULL |
| 112 | NULL |
| 113 | NULL |
| 113 | NULL |
| 114 | NULL |
| 114 | NULL |
+------------------+------+
delete servers_links FROM servers_links LEFT JOIN servers ON servers_links.servers_links_id=servers.id where servers.name IS NULL;
Query OK, 20 rows affected (0.02 sec)
我们上面有20条记录在桌子上,映射到服务器表中的非现有条目。
使用上面的删除命令,它现在删除了那些错误的条目。
我在多个数据库关系中使用了上述公式,它的工作方式相同,只需将字段替换为与您的表匹配的字段与冲突