我目前正在尝试从Hibernate生成我的ddl方案切换到Flyway迁移。我通过这种方式生成了我的V1__Initial. sql脚本。当我在内存H2数据库上运行它时,一切正常。但是当我尝试在MariaDB上运行它时,我得到了以下异常:
Migration V1__Initial.sql failed
--------------------------------
SQL State : HY000
Error Code : 1005
Message : (conn=130) Can't create table `booking`.`booking_cancelled_event` (errno: 150 "Foreign key constraint is incorrectly formed")
Location : db/migration/V1__Initial.sql (C:\code\ajt\backend\target\classes\db\migration\V1__Initial.sql)
Line : 463
Statement : alter table booking_cancelled_event
add constraint FKnxiyj0m730pl9ol2y4qng7577
foreign key (cancelled_booking_id)
references cancelled_internal_booking
V1__Initial.sql中的相应部分:
alter table booking_cancelled_event
add constraint FKnxiyj0m730pl9ol2y4qng7577
foreign key (cancelled_booking_id)
references cancelled_internal_booking;
SHOW ENGINE InnoDB STATUS
的输出;:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2019-06-23 21:07:30 0x1974 Error in foreign key constraint of table `xxx`.`booking_cancelled_event`:
Alter table `booking`.`booking_cancelled_event` with foreign key constraint failed. Parse error in '
foreign key (cancelled_booking_id)
references cancelled_internal_booking' near '
references cancelled_internal_booking'.
版本:
> < li>
MariaDB 10.4
Spring Boot 2.1.5版本
飞道5.2.4
根据MariaDB文档的ALTER TABLE页面,< code>ADD CONSTRAINT的语法应该是:
ADD [CONSTRAINT [symbol]]
FOREIGN KEY [IF NOT EXISTS] [index_name] (index_col_name,...)
reference_definition
根据 MariaDB 文档的 CREATE TABLE 页面,reference_definition
的语法应该是:
REFERENCES tbl_name (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
所以您遇到的解析错误很可能是因为您遗漏了一个< code>(index_col_name,...)在< code > cancelled _ internal _ booking 表名之后。