无法让liqubase生成具有多个模式的外键

时间:2017-03-03 13:54:13

标签: liquibase

我有多个模式(2),并且在生成表时,它无法在表之间生成FK。 所有FK都在一个架构中。当我删除FK时,它生成就好了。

我将其作为

运行
mvn clean liquibase:update -Dt1Schema=t1 -Dt2Schema=t2

我尝试了什么

<changeSet id="0" author="admin">
    <createTable tableName="t1" remarks="t1" schemaName="${t1Schema}">
        <column name="id" type="BIGINT" autoIncrement="true" remarks="id">
            <constraints primaryKey="true" nullable="false"/>
        </column>
    </createTable>

    <createTable tableName="t2" remarks="t2" schemaName="${t1Schema}">
        <column name="id" type="BIGINT" autoIncrement="true" remarks="id">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="t1_id" type="BIGINT" remarks="t1 id">
            <constraints nullable="true" deferrable="true" foreignKeyName="fk_t1_t2" referencedTableName="${t1Schema}.t1" referencedColumnNames="id"/>
        </column>
    </createTable>
</changeSet>

我明白了:

  

错误:关系“teeregister.t1”不存在[失败的SQL:CREATE TABLE teeregister.t2 ...

<changeSet id="0" author="admin">
    <createTable tableName="t1" remarks="t1" schemaName="${t1Schema}">
        <column name="id" type="BIGINT" autoIncrement="true" remarks="id">
            <constraints primaryKey="true" nullable="false"/>
        </column>
    </createTable>

    <createTable tableName="t2" remarks="t2" schemaName="${t1Schema}">
        <column name="id" type="BIGINT" autoIncrement="true" remarks="id">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="t1_id" type="BIGINT" remarks="t1 id">
            <constraints nullable="true"/>
        </column>
    </createTable>
</changeSet>

<changeSet id="1" author="admin">
    <addForeignKeyConstraint baseColumnNames="t1_id" baseTableName="${t1Schema}.t2" constraintName="fk_t1_t2" referencedColumnNames="id" referencedTableName="${t1Schema}.t1"/>
</changeSet>

我明白了:

  

错误:关系“teeregister.t2”不存在[失败的SQL:ALTER TABLE“teeregister.t2”ADD CONSTRAINT ...

pom.xml配置

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <changeLogFile>src/main/resources/schema1.xml</changeLogFile>
        <changeLogFile>src/main/resources/schema2.xml</changeLogFile>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://localhost:5432/postgres</url>
        <username>postgres</username>
        <password>postgres</password>
    </configuration>
</plugin>

1 个答案:

答案 0 :(得分:2)

我需要做的就是阅读一些文档......

这很好用

<addForeignKeyConstraint baseColumnNames="t1_id" baseTableSchemaName="${t1Schema}" baseTableName="t2" constraintName="fk_t1_t2" referencedColumnNames="id" referencedTableSchemaName="${t1Schema}" referencedTableName="t1"/>