【问题标题】:liquibase constraints references on create table创建表上的 liquibase 约束引用
【发布时间】:2017-05-28 06:25:53
【问题描述】:

我正在从 Oracle 迁移到 Postgresql。 我们已经迁移到 Liquibase 有一段时间了,但不是从一开始就迁移到 Liquibase。现在我正在添加和调整迁移以完全部署数据库并在那里传输数据。

现在我遇到了这样一个问题,即我没有创建一个包含与另一个表相关联的约束的列的表。由于在约束中的表之前明确指定了方案,因此没有采用此限制。

如何在未指定方案的表的约束中指定使用默认方案?

使用 Maven 插件 liquibase-maven-插件 3.5.3

这里是属性:

Url: jdbc: postgresql: //192.168.1.1: 5432 / postgres
DefaultSchemaName: ist
ReferencedTableSchemaName: ist
Username: test
Password: 123
Verbose: true
DropFirst: false

这是迁移片段

 - createTable:
        TableName: opr_possibilities
        Remarks: Operator capability map
        Columns:
        - column:
            Name: id
            Type: number (11)
            Remarks: ID
            Constraints:
              Nullable: false
              PrimaryKey: true
        - column:
            Name: operator_id
            Type: number (11)
            Remarks: Operator ID
            Constraints:
              Nullable: false
              ForeignKeyName: fk_possibility_operator
              References: ds_obj_opr (id)
# ReferencedTableName: ds_obj_opr
# ReferencedColumnNames: id

迁移产生这样的请求:

CREATE TABLE ist.opr_possibilities (
      id numeric(11) NOT NULL, 
      operator_id numeric(11) NOT NULL, 
      begin_date date DEFAULT NOW() NOT NULL, 
      end_date date DEFAULT NOW() NOT NULL, 
      duty BOOLEAN DEFAULT FALSE NOT NULL, 
      status numeric(4) DEFAULT 0 NOT NULL, 
      type numeric(4) DEFAULT 0 NOT NULL, 
      remarks VARCHAR(255), 
      CONSTRAINT PK_OPR_POSSIBILITIES PRIMARY KEY (id), 
      CONSTRAINT fk_possibility_operator FOREIGN KEY (operator_id) REFERENCES ds_obj_opr(id)
)

告诉我如何优雅地解决这个问题。 谢谢。

【问题讨论】:

    标签: postgresql maven migration constraints liquibase


    【解决方案1】:

    你可以说

    References: ist.ds_obj_opr(id)
    

    但我猜你不认为那是优雅的……

    或者,您可以将架构指定为参数,例如

    References: ${schema}.ds_obj_opr(id)
    

    如果您在任何地方都这样做,您可以使用DefaultSchemaName,而是在更改日志中使用<property> 标记或在调用期间使用-Dschema=ist 指定架构。

    【讨论】:

    • 是的,你是对的,我不认为这些决定是优雅的)但我认为这个问题是一个错误。为什么“引用”不使用“defaultSchemaName”?就像 "addForeignKeyConstraint" "constraintName" 不关注 "objectQuotingStrategy" = "QUOTE_ALL_OBJECTS" 但是现在我通过变量应用了模式指定,因为在我们的迁移中使用了本机查询。但是感谢您的回答)
    猜你喜欢
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 2021-12-30
    • 1970-01-01
    • 2015-12-10
    相关资源
    最近更新 更多