Liquibase error: "relation does not exist" when creating main table and table with foreign key in one change set

Liquibase error: "relation does not exist" when creating main table and table with foreign key in one change set

我正在尝试在 java springboot 应用程序中使用 liquibase 和 postgresql 创建两个具有一对多关系的 table。变更日志中只有一个条目。

启动应用程序时我收到此错误:

liquibase.exception.DatabaseException: ERROR: relation "public.main" does not exist [Failed SQL: (0) CREATE TABLE public.relation (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, main_id BIGINT NOT NULL, CONSTRAINT "PK_relation_id" PRIMARY KEY (id), CONSTRAINT "FK_relation_main" FOREIGN KEY (main_id) REFERENCES public.main(id), UNIQUE (id))]

表定义:

{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "initial-structure-create",
        "author": "tcyborowski",
        "changes": [
          {
            "createTable": {
              "tableName": "main",
              "columns": [
                {
                  "column": {
                    "name": "id",
                    "type": "bigint",
                    "autoIncrement": true,
                    "constraints": {
                      "primaryKey": true,
                      "primaryKeyName": "PK_main_id",
                      "nullable": false,
                      "unique": true
                    }
                  }
                },
                {
                  "column": {
                    "name": "name",
                    "type": "varchar(255)",
                    "constraints": {
                      "nullable": false,
                      "unique": false
                    }
                  }
                }
              ]
            },
            "createTable": {
              "tableName": "relation",
              "columns": [
                {
                  "column": {
                    "name": "id",
                    "type": "bigint",
                    "autoIncrement": true,
                    "constraints": {
                      "primaryKey": true,
                      "primaryKeyName": "PK_relation_id",
                      "nullable": false,
                      "unique": true
                    }
                  }
                },
                {
                  "column": {
                    "name": "main_id",
                    "type": "bigint",
                    "constraints": {
                      "nullable": false,
                      "unique": false,
                      "referencedTableSchemaName": "public",
                      "referencedTableName": "main",
                      "referencedColumnNames": "id",
                      "foreignKeyName": "FK_relation_main"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

liquidbase 版本 4.3.3 - h2 内存数据库也有同样的问题

如果我将此 json tables 声明拆分为两个 json 更改文件,运行 第一个 table 创建,然后是第二个 table 创建 table 的正确关系没有问题。

如果我在干净的数据库上尝试 运行 这个,我会得到与上面相同的错误:

{
  "databaseChangeLog": [
    {
      "include": {
        "file": "db/changelog/partials/initial-structure.json"
      },
      "include": {
        "file": "db/changelog/partials/initial-structure2.json"
      }
    }
  ]
}

一次性 运行 解决这个问题的原因是什么?

问题是changeset是以事务方式执行的。更改将在 changeset

结束时提交

我的建议是在两个不同的 changesets.

中创建两个表

Liquibase attempts to execute each changeset in a transaction that is committed at the end, or rolled back if there is an error. Some databases will auto-commit statements which interferes with this transaction setup and could lead to an unexpected database state. Therefore, it is best practice to have just one change per changeset unless there is a group of non-auto-committing changes that you want to apply as a transaction such as inserting data.

好的,将日志记录更改为调试后我发现了我的错误。

在我上面的两个 json 文件中都有一个错误导致只执行最后一个操作。

更正了以下 json 个文件:

{
  "databaseChangeLog": [
    {
      "include": {
        "file": "db/changelog/partials/initial-structure.json"
      }
    },
    {
      "include": {
        "file": "db/changelog/partials/initial-structure2.json"
      }    
    }
  ]
}
{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "initial-structure-create",
        "author": "tcyborowski",
        "changes": [
          {
            "createTable": {
              "tableName": "main",
              "columns": [
                {
                  "column": {
                    "name": "id",
                    "type": "bigint",
                    "autoIncrement": true,
                    "constraints": {
                      "primaryKey": true,
                      "primaryKeyName": "PK_main_id",
                      "nullable": false,
                      "unique": true
                    }
                  }
                },
                {
                  "column": {
                    "name": "name",
                    "type": "varchar(255)",
                    "constraints": {
                      "nullable": false,
                      "unique": false
                    }
                  }
                }
              ]
            }
          },
          {
            "createTable": {
              "tableName": "relation",
              "columns": [
                {
                  "column": {
                    "name": "id",
                    "type": "bigint",
                    "autoIncrement": true,
                    "constraints": {
                      "primaryKey": true,
                      "primaryKeyName": "PK_relation_id",
                      "nullable": false,
                      "unique": true
                    }
                  }
                },
                {
                  "column": {
                    "name": "main_id",
                    "type": "bigint",
                    "constraints": {
                      "nullable": false,
                      "unique": false,
                      "referencedTableSchemaName": "public",
                      "referencedTableName": "main",
                      "referencedColumnNames": "id",
                      "foreignKeyName": "FK_relation_main"
                    }
                  }
                }
              ]
            }
          }          
        ]
      }
    }
  ]
}