You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Liquibase同一变更集创建主表和外键关联表报relation不存在错误如何解决

问题原因分析

你遇到的问题本质是JSON语法规则导致的配置丢失,和Liquibase版本、数据库选型都没有关系。

1. 单变更集配置错误原因

JSON语法规定,同一个对象内不允许出现重复的键名,如果出现重复键,后定义的键值会直接覆盖先定义的键值。
你给出的单变更集配置中,changes数组的第一个对象内同时定义了两个createTable键:第一个对应main表的创建配置,第二个对应relation表的创建配置。JSON解析时,第一个createTable的配置会被直接覆盖,Liquibase实际只会拿到创建relation表的逻辑,完全不会执行main表的创建,所以创建relation表时外键关联自然找不到public.main表。

2. 多文件合并配置错误原因

你给出的多文件合并配置同样存在重复键问题:databaseChangeLog数组的第一个对象内同时定义了两个include键,JSON解析后只有第二个include对应的配置会生效。如果第二个文件是relation表的创建逻辑,同样会出现只执行relation表创建、找不到main表的错误。


正确写法示例

单变更集双表创建正确配置

将两个createTable放在changes数组的两个独立对象中即可:

{
  "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"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

多文件引入正确配置

将两个include放在databaseChangeLog数组的两个独立对象中即可:

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

内容的提问来源于stack exchange,提问作者Tomasz Cyborowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 09:12:04