FlywaySqlException:无法在架构历史记录 table `schema_version` 中插入版本 `11` 的行:字段 `version_rank` 没有默认值

FlywaySqlException: Unable to insert row for version `11` in Schema History table `schema_version`: Field `version_rank` doesn't have a default value

我在 运行 我的 Spring 使用新迁移启动应用程序时遇到此错误。到目前为止,它已经完成了 10 次迁移。该字段确实没有默认值。不需要默认值,因为 Flyway 应该在该字段中插入值 11。

Caused by: org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to insert row for version '11' in Schema History table `app`.`schema_version`
--------------------------------------------------------------------------------------------
SQL State  : HY000
Error Code : 1364
Message    : Field 'version_rank' doesn't have a default value

    at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.doAddAppliedMigration(JdbcTableSchemaHistory.java:174) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.schemahistory.SchemaHistory.addAppliedMigration(SchemaHistory.java:170) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.command.DbMigrate.applyMigrations(DbMigrate.java:299) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.command.DbMigrate.migrateGroup(DbMigrate.java:244) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.command.DbMigrate.access0(DbMigrate.java:53) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.command.DbMigrate.call(DbMigrate.java:163) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.command.DbMigrate.call(DbMigrate.java:160) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.database.mysql.MySQLNamedLockTemplate.execute(MySQLNamedLockTemplate.java:60) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.database.mysql.MySQLConnection.lock(MySQLConnection.java:80) ~[flyway-core-5.1.4.jar:na]
    at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.lock(JdbcTableSchemaHistory.java:150) ~[flyway-core-5.1.4.jar:na]
pom.xml
<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
  <version>5.1.4</version><!--$NO-MVN-MAN-VER$-->
</dependency>
application.properties
# Prevent complaints when starting migrations with existing tables.
flyway.baselineOnMigrate = true
flyway.table=schema_version
schema_version
| Field          | Type          | Null | Key | Default           | Extra |
+----------------+---------------+------+-----+-------------------+-------+
| version_rank   | int(11)       | NO   | MUL | NULL              |       |
| installed_rank | int(11)       | NO   | MUL | NULL              |       |
| version        | varchar(50)   | NO   | PRI | NULL              |       |
| description    | varchar(200)  | NO   |     | NULL              |       |
| type           | varchar(20)   | NO   |     | NULL              |       |
| script         | varchar(1000) | NO   |     | NULL              |       |
| checksum       | int(11)       | YES  |     | NULL              |       |
| installed_by   | varchar(100)  | NO   |     | NULL              |       |
| installed_on   | timestamp     | NO   |     | CURRENT_TIMESTAMP |       |
| execution_time | int(11)       | NO   |     | NULL              |       |
| success        | tinyint(1)    | NO   | MUL | NULL              |       |

如果迁移甚至不起作用,我如何创建迁移以添加默认值?

Flyway 5.1.4,Spring Boot 5.1.13,mysql Ver 15.1 Distrib 10.1.30-MariaDB,适用于 CYGWIN (i686)

不得不

  • 通过将默认值写入已经 运行 但不会保存

    的迁移顶部,手动将默认值添加到字段
    alter table schema_version alter column version_rank set default 0;
    
  • 评论迁移中的其他行,因为它们已经 运行

    -- alter table myTable add column createdAt date;
    -- ...
    
  • 再次迁移以将行添加到 schema_version

    mvn flyway:migrate -Dflyway.user=user -Dflyway.password= -Dflyway.url=jdbc:mysql://localhost:3306/app -Dflyway.table=schema_version
    
  • 取消注释迁移中的行,以便将它们应用到管道中

  • 验证

    mvn flyway:validate -Dflyway.user=user -Dflyway.password= -Dflyway.url=jdbc:mysql://localhost:3306/app -Dflyway.table=schema_version
    
    [ERROR] -> Applied to database : 1445435712
    [ERROR] -> Resolved locally    : -1275756780
    
  • 手动将校验和复制到 schema_version table 这样它就不会再在开发中抱怨了

    update schema_version set checksum = -1275756780 where version = 11;
    

希望它能在演出和制作中发挥作用。

也许 FlyWay 应该使用 Rails 迁移来管理自己的模式?

我知道数据库版本控制是一个难题,FlyWay 应该可以使它变得更容易,但有时感觉它比仅使用自定义 SQL 脚本或手动部署检查表更费力。我找不到任何关于这个问题的提及,我的最后一次迁移是在 7 月底左右。也许现在每个人都停止使用它了?我从 5 月才开始使用它,遇到了很多问题(table 名称更改、校验和计算更改,现在是这个)。

如果您从 Flyway 3.x 直接升级到 5.x,绕过 4.x,似乎会出现此问题。

Spring Boot 1.5 默认使用 Flyway 3.x,而 Spring Boot 2.x 使用 Flyway 5.x.

来自Flyway 5.0.0 release notes

Important note for users upgrading from Flyway 3.x: This release no longer supports a schema history table upgrade from Flyway 3.x. You must upgrade to Flyway 4.2.0 first before upgrading to Flyway 5.0.0.

来自Spring Boot 2 Migration Guide

Upgrading to Spring Boot 2 will upgrade Flyway from 3.x to 5.x. To make sure that the schema upgrade goes smoothly, please follow the following instructions:

  • First upgrade your 1.5.x Spring Boot application to Flyway 4 (4.2.0 at the time of writing), see the instructions for Maven and Gradle.

  • Once your schema has been upgraded to Flyway 4, upgrade to Spring Boot 2 and run the migration again to port your application to Flyway 5.

迁移指南还通过另一种方法链接到 a blog post

有人提出了关于 Spring Boot skipping Flyway 4.x and one against Flyway for the version_rank exception 你得到的问题。

看起来 遇到了同样的问题。