英文:
Liquibase bidirectional relationships changelog order
问题
我在现有项目中实施 liquibase 时遇到了问题。所以我们有两个实体:
Company:
@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;
}
}
Stock:
@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 changelogs。
Company:
<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>
Stock:
<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>
</databaseChangeLog>
还有 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"/>
</databaseChangeLog>
我遇到了这个异常:
Caused by: org.postgresql.util.PSQLException: ERROR: relation "public.company" does not exist
我理解 liquibase 在尝试创建 stock 表,然后添加 company_id 作为外键,尽管还没有 company 表。我有很多双向关系,这只是其中的一部分。
问题:我应该如何组织 liquibase changeLogs,以便在一个构建中创建所有表和它们之间的关系?我是否应该创建类似于 "db.changelogs.relations" 的内容,在其中设置外键?
UPD:已关闭。使用 Maven 插件为 liquibase 生成您的 changeLogs。
英文:
I've faced a problem when implementing liquibase in an existing project. So we have two entities:
Company:
@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;
}
}
Stock:
@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;
}
}
And my liquibase changelogs.
Company:
<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>
Stock:
<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>
</databaseChangeLog>
And 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"/>
</databaseChangeLog>
I have this exception:
Caused by: org.postgresql.util.PSQLException: ERROR: relation "public.company" does not exist
I understand that liquibase trying to create stock and then add company_id as foreign key although there is no company table yet. I have a bunch of bidirectional relationships and this one is only part of them.
Question: How can I organize liquibase changeLogs to create all tables and relationships between them in one build? Should I create something like "db.changelogs.relations" where I will set up foreign keys?
UPD: Closed. Generate your changeLogs with maven plugin for liquibase
答案1
得分: 1
将我的问题评论整理成回答:
看起来你正在手动编写你的变更日志 XML。如果你调整变更日志的顺序,将表的创建放在前面,然后是外键,那就没问题了。
- 创建 Table1、Table2、Table3
- 在 Table1 和 Table2 之间建立关系,在 Table2 和 Table3 之间建立关系
更好的选择是使用 Liquibase 插件(适用于你的 Maven/Gradle),以生成这种变更日志。Liquibase 会先为表生成变更日志,然后是外键,这样你就不用担心这个问题了。
可以参考以下链接:
- https://github.com/gtiwari333/spring-boot-web-application-sample/blob/master/main-app/main-orm/pom.xml
- https://github.com/gtiwari333/spring-boot-web-application-sample/tree/master/main-app/main-orm/src/main/resources/liquibase/changelog
英文:
Creating Answer out of my Comments on the Question:
It looks you are writing your change log xml manually. If you arrange your change log to put table creation at first and the FKs then it would be fine.
- Create Table1, Table2, Table3
- Setup relation between Table1 and Table2, Table2 an Table3
Better option is to use liquibase plugin (for your maven/gradle) to generate such change logs. Liquibase would generate the change logs for tables first and then the FKs so that you don't need to worry about this.
See these as an reference:
答案2
得分: 0
代码部分不要翻译。
英文:
The include order needs to be switched in the master change log file. The company table isn't created by the time the db.changelog.stock.xml file is executed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论