Liquibase 在一对一关系中创建 table 模式
Liquibase create table schemas in one-to-one relationship
我正在尝试编写一个 Liquibase 脚本来创建 2 个具有一对一关系的 table。它失败了异常:
ERROR: relation "owner" does not exist
我认为发生这种情况是因为在创建 CAR
table 时,OWNER
table 仍然不存在。
<changeSet id="001" author="wesleyy">
<createTable tableName="CAR">
<column name="ID" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="OWNER_ID" type="bigint">
<constraints
foreignKeyName="OWNER_ID"
references="OWNER(ID)" />
</column>
</createTable>
<createTable tableName="OWNER">
<column name="ID" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="CAR_ID" type="bigint">
<constraints
foreignKeyName="CAR_ID"
references="CAR(ID)" />
</column>
</createTable>
</changeSet>
</databaseChangeLog>
问题是:我是否正确使用了外键引用?我如何在 Liquibase 中实现这一目标?
循环外键约束通常不是一个好主意。但如果你是 100%,你就需要它。您需要先创建两个表,然后 然后 添加外键
<changeSet id="001" author="wesleyy">
<createTable tableName="car">
<column name="id" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="owner_id" type="bigint"/>
</createTable>
<createTable tableName="owner">
<column name="id" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="car_id" type="bigint"/>
</createTable>
<addForeignKeyConstraint constraintName="fk_owner2car"
baseTableName="owner"
baseColumnNames="car_id"
referencedTableName="car"
referencedColumnNames="id"/>
<addForeignKeyConstraint constraintName="fk_car2owner"
baseTableName="car"
baseColumnNames="owner_id"
referencedTableName="owner"
referencedColumnNames="id"/>
</changeSet>
无关,但是:
在将 Liquibase 与 Postgres 一起使用时,您不应使用大写字母编写标识符。 Liquibase 认为您需要区分大小写的大写标识符,并且会将它们全部用双引号引起来,使它们区分大小写。使用带引号的标识符几乎总是比它们的价值更麻烦。
我正在尝试编写一个 Liquibase 脚本来创建 2 个具有一对一关系的 table。它失败了异常:
ERROR: relation "owner" does not exist
我认为发生这种情况是因为在创建 CAR
table 时,OWNER
table 仍然不存在。
<changeSet id="001" author="wesleyy">
<createTable tableName="CAR">
<column name="ID" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="OWNER_ID" type="bigint">
<constraints
foreignKeyName="OWNER_ID"
references="OWNER(ID)" />
</column>
</createTable>
<createTable tableName="OWNER">
<column name="ID" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="CAR_ID" type="bigint">
<constraints
foreignKeyName="CAR_ID"
references="CAR(ID)" />
</column>
</createTable>
</changeSet>
</databaseChangeLog>
问题是:我是否正确使用了外键引用?我如何在 Liquibase 中实现这一目标?
循环外键约束通常不是一个好主意。但如果你是 100%,你就需要它。您需要先创建两个表,然后 然后 添加外键
<changeSet id="001" author="wesleyy">
<createTable tableName="car">
<column name="id" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="owner_id" type="bigint"/>
</createTable>
<createTable tableName="owner">
<column name="id" type="bigint">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="car_id" type="bigint"/>
</createTable>
<addForeignKeyConstraint constraintName="fk_owner2car"
baseTableName="owner"
baseColumnNames="car_id"
referencedTableName="car"
referencedColumnNames="id"/>
<addForeignKeyConstraint constraintName="fk_car2owner"
baseTableName="car"
baseColumnNames="owner_id"
referencedTableName="owner"
referencedColumnNames="id"/>
</changeSet>
无关,但是:
在将 Liquibase 与 Postgres 一起使用时,您不应使用大写字母编写标识符。 Liquibase 认为您需要区分大小写的大写标识符,并且会将它们全部用双引号引起来,使它们区分大小写。使用带引号的标识符几乎总是比它们的价值更麻烦。