有没有办法以正确的顺序生成Liquibase数据? [英] Is there a way to generate Liquibase data in the right order?

查看:128
本文介绍了有没有办法以正确的顺序生成Liquibase数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Liquibase对现有数据库进行版本控制,所以我正在使用

I'm using Liquibase for versioning an existing database, so I'm using

liquibase \
  --logLevel=debug \
  --driver=com.mysql.jdbc.Driver \
  --classpath=lib/mysql-connector-java-5.1.30.jar \
  --url="jdbc:mysql://127.0.0.1:3306/schema" \
  --username=user \
  --password=pass \
  --diffTypes="data" \
  --changeLogFile="./data.xml" generateChangeLog

用于生成变更集xml.

for generating the changeset xml.

这可行,但是问题是当我尝试运行那些生成的变更集时.我知道了

This works, but the problem is when I'm trying to run those generated changesets. I get

无法添加或更新子行:外键...",因为更改集的导出顺序未考虑外键.

Cannot add or update a child row: a foreign key ...' because the exported order of changeset does not take into account the foreign keys.

我的问题是:是否有命令选项或某些可以按正确顺序生成变更集的东西,还是应该手动重新排列变更集以获得所需的结果?

My question is: Is there a command option or something that can generate the changeset in the right order or should I manually reorder the changesets to get the desired result?

更新:

通常,外键应该是在外键之后创建的.但是,在我们的系统中,核心应用程序创建了数据库的结构,并且多个客户端应用程序在同一个数据库中使用自己的私有数据填充数据库.当我们生成数据时,数据更改集是按数据库中表的字母顺序生成的,这可能是外键约束.我们设法手动安排了变更集,但是我想知道在这种情况下是否有更好的解决方法.

Normally, the foreign keys should be and are created after the foreign key. But, in our systems, the core application creates the structure of the database and multiple client applications are populating the database with their own private data in the same database. And when we're generating the data, the data changesets are generated in the alphabetical order of the tables in the database, which may the foreign key constraints. We managed to manually arrange the changesets, but I would like to know if there is a nicer workaround for this particular situation.

推荐答案

所以问题不在于更改类型的顺序(表,然后是数据,然后是FK),而是您仅在现有的情况下使用生成的数据插图表和FK结构? Liquibase甚至没有尝试弄清行之间如何相互依赖,因为在一般情况下行几乎变得不可能.

So the problem is not with the order of types of changes (tables, then data then FKs) but that you are using just the generated data insets with an existing table and FK structure? Liquibase does not even try to figure out how rows depend on each other as it gets nearly impossible in the general case.

最简单的解决方案是在插入之前禁用FK检查,然后在之后重新启用它们.具体操作方式取决于您的数据库,但是您可以包含以下内容:

The easiest solution is to disable FK checks before inserting and re-enable them after. How you do that depends on your database, but you can include something like:

<changeSet id="disable-keys" author="x" runAlways="true">
    <sql>SET FOREIGN_KEY_CHECKS=0;</sql>
</changeSet>

在您的<insert>标记和

<changeSet id="enable-keys" author="x" runAlways="true">
    <sql>SET FOREIGN_KEY_CHECKS=1;</sql>
</changeSet>

在他们之后.

这篇关于有没有办法以正确的顺序生成Liquibase数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆