-5

错误:

| 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)
4

1 回答 1

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 条记录,这些记录映射到服务器表中不存在的条目。

使用上面的删除命令,它现在已经删除了那些坏条目。

我在多个数据库关系上使用了上述公式,它的工作原理相同,只需将字段替换为与您的表匹配的字段即可

于 2013-08-09T10:03:01.733 回答