Liquibase:添加外键约束时指定模式

时间:2018-03-07 23:20:03

标签: postgresql liquibase

我使用liquibase 3.5.5和postgresql。我的所有表都是在指定的模式中创建的,但是当liquibase尝试生成DDL时,它会附加" public。"在referencesTableName前面。我试过制作表名" identity.users",但它只是试图创建公共。" identity.users"。

例如,下面的

<createTable tableName="users" schemaName="identity">
            <column name="id" type="bigint" defaultValueSequenceNext="seq_users">
                <constraints primaryKey="true" primaryKeyName="pk_users"/>
            </column>
</createTable>
<createTable tableName="external_identities" schemaName="identity">
            <column name="id" type="bigint" defaultValueSequenceNext="seq_external_identities">
                <constraints primaryKey="true" primaryKeyName="pk_external_identities"/>
            </column>
            <column name="user_id" type="bigint">
                <constraints foreignKeyName="fk_external_identities_users" referencedTableName="users" referencedColumnNames="id" nullable="false"/>
            </column>
</createTable>

制作:

 CREATE TABLE identity.external_identities (id BIGINT DEFAULT nextval('identity.seq_external_identities') NOT NULL, user_id BIGINT NOT NULL, CONSTRAINT fk_external_identities_users FOREIGN KEY (user_id) REFERENCES public.users(id))

文档帮助不大,XSD文件不会将模式列为选项:

<!-- Attributes for constraints -->
    <xsd:attributeGroup name="constraintsAttributes">
        <xsd:attribute name="nullable" type="booleanExp" />
        <xsd:attribute name="primaryKey" type="booleanExp" />
        <xsd:attribute name="primaryKeyName" type="xsd:string" />
        <xsd:attribute name="primaryKeyTablespace" type="xsd:string" />
        <xsd:attribute name="unique" type="booleanExp" />
        <xsd:attribute name="uniqueConstraintName" type="xsd:string" />
        <xsd:attribute name="references" type="xsd:string" />
        <xsd:attribute name="referencedTableName" type="xsd:string"/>
        <xsd:attribute name="referencedColumnNames" type="xsd:string"/>
        <xsd:attribute name="foreignKeyName" type="xsd:string" />
        <xsd:attribute name="deleteCascade" type="booleanExp" />
        <xsd:attribute name="deferrable" type="booleanExp" />
        <xsd:attribute name="initiallyDeferred" type="booleanExp" />
        <xsd:attribute name="checkConstraint" type="xsd:string" />
    </xsd:attributeGroup>

2 个答案:

答案 0 :(得分:1)

必须将changeSet与sql一起使用:              ALTER TABLE schema.table ADD CONSTRAINT约束FOREIGN KEY(fk_field)参考schema.ref_table(field)     

答案 1 :(得分:0)

尝试使用单独的changeset添加外键constratint。它将是addForeignKeyConstraint。它具有架构属性。

ChangeSet将如下所示:

<changeSet author="liquibase-docs" id="addForeignKeyConstraint-example">
    <addForeignKeyConstraint
            baseColumnNames="user_id"
            baseTableName="external_identities"
            constraintName="fk_external_identities_users"
            referencedColumnNames="id"
            referencedTableName="users"
            baseTableSchemaName="identity"
            referencedTableSchemaName="identity"/>
</changeSet>