Flyway和MariaDB:SQLException外键约束格式错误

时间:2019-06-23 19:26:03

标签: hibernate spring-boot jpa mariadb flyway

我目前正尝试从生成我的ddl方案的Hibernate切换到Flyway迁移。我生成了V1__Initial.sql脚本by this means。当我在内存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 `spozing`.`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'.

版本:

  • MariaDB 10.4

  • Spring Boot 2.1.5.RELEASE

  • Flyway 5.2.4

1 个答案:

答案 0 :(得分:2)

根据MariaDB文档的ALTER TABLE页,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]

因此,您遇到的解析错误很可能是因为您错过了(index_col_name,...)表名之后的cancelled_internal_booking