Liquibase 双向关系变更日志顺序

Liquibase bidirectional relationships changelog order

我在现有项目中实施 liquibase 时遇到问题。所以我们有两个实体:

公司:

@Entity
@Table(name = "company")
@EqualsAndHashCode(of = {}, callSuper = true)
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Company extends AbstractAccount {

    private String organizationName;
    private String mail;
    private Set<Stock> stocks;
    private Stock currentStock;

    @Column(name = "organization_name", unique = true)
    public String getOrganizationName() {
        return organizationName;
    }

    @Email
    @Column(name = "mail", unique = true)
    public String getMail() {
        return mail;
    }

    @Cascade({CascadeType.REMOVE, CascadeType.SAVE_UPDATE})
    @OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
    public Set<Stock> getStocks() {
        return stocks;
    }

    @OneToOne
    @JoinColumn(name = "current_stock_id", referencedColumnName = "id")
    public Stock getCurrentStock() {
        return currentStock;
    } 
}

股票:

@Entity
@Table(name = "stock")
@EqualsAndHashCode(of = {}, callSuper = true)
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Stock extends AbstractEntity {

    private String name;
    private Company company;
    private Double fare;

    @Column(name = "panel")
    public Double getFare() {
        return fare;
    }

    @NotBlank(message = "Название акции не может быть пустым.")
    @Column(name = "name")
    public String getName() {
        return name;
    }

    @Cascade({CascadeType.SAVE_UPDATE})
    @ManyToOne(fetch = FetchType.LAZY)
    public Company getCompany() {
        return company;
    }

}

还有我的 liquibase 更新日志。

公司:

<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="1" author="Maxim Grankin">
    <createTable tableName="company">
        <column name="id" type="bigint" autoIncrement="true">
            <constraints primaryKey="true" nullable="false"/>
        </column>
    </createTable>
</changeSet>

<changeSet id="2" author="Maxim Grankin">
    <addColumn tableName="company">
        <column name="organizationName" type="varchar(255)">
            <constraints unique="true"/>
        </column>
        <column name="mail" type="varchar(255)">
            <constraints unique="true"/>
        </column>
        <column name="current_stock_id" type="bigint"/>
    </addColumn>
    <addForeignKeyConstraint baseTableName="company" baseColumnNames="current_stock_id"
                             constraintName="fkoi5aq2bm82091ubh15kfj29m1"
                             referencedTableName="stock"
                             referencedColumnNames="id"/>
</changeSet>

股票:

<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="1" author="Maxim Grankin">
    <createTable tableName="stock">
        <column name="id" type="bigint" autoIncrement="true">
            <constraints primaryKey="true" nullable="false"/>
        </column>
    </createTable>
</changeSet>

<changeSet id="2" author="Maxim Grankin">
    <addColumn tableName="stock">
        <column name="name" type="varchar(255)"/>
        <column name="panel" type="double precision"/>
        <column name="company_id" type="bigint"/>
    </addColumn>
    <addForeignKeyConstraint baseTableName="stock" baseColumnNames="company_id"
                             constraintName="fk9r297vk0rghnrccw09x0qybfj"
                             referencedTableName="company"
                             referencedColumnNames="id"/>
</changeSet>

master:

<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

<include file="data/changelogs/stock/stock/db.changelog.stock.xml"/>
<include file="data/changelogs/company/db.changelog.company.xml"/>

我有这个例外:

Caused by: org.postgresql.util.PSQLException: ERROR: relation "public.company" does not exist

我知道 liquibase 试图创建库存然后添加 company_id 作为外键,尽管还没有公司 table。我有一堆双向关系,这只是其中的一部分。

问题:我如何组织 liquibase changeLogs 以在一个构建中创建所有 tables 和它们之间的关系?我应该创建类似“db.changelogs.relations”的东西来设置外键吗?

UPD:关闭。使用 liquibase

的 maven 插件生成你的 changeLogs

需要在主更改日志文件中切换包含顺序。公司 table 未在执行 db.changelog.stock.xml 文件时创建。

根据我对问题的评论创建答案:

看起来您正在手动编写更改日志 xml。如果您安排您的更改日志首先放置 table 创建和 FK,那么就可以了。

  • 创建表 1、表 2、表 3
  • 设置表1和表2、表2和表3的关系

更好的选择是使用 liquibase 插件(针对您的 maven/gradle)生成此类更改日志。 Liquibase 将首先为 table 生成更改日志,然后生成 FK,因此您无需担心这一点。

查看这些作为参考: