【问题标题】:LiquiBase changeset: how do build a where clause based on ANOTHER table's columnLiquiBase 变更集:如何基于另一个表的列构建 where 子句
【发布时间】:2022-06-30 16:45:08
【问题描述】:

基于问题How to build a WHERE-clause in a LiquiBase changeset,我想根据不同表中的列的值来选择列的默认值。

例如,我有两个表,orderorder_history。请注意,我们在 order_history 中的 order 中维护所有订单。订单完成后,将从 order 表中删除。

order 有一个“状态”列,但我们在 order_history 上错过了它,你可以称之为糟糕的设计。 我现在如何将“状态”添加到 order_history,并从相应订单中复制值 升级时现有数据的 order 表中?有没有办法在 liquibase 中做到这一点?

【问题讨论】:

    标签: where-clause liquibase changeset


    【解决方案1】:

    如果orderorder_history用外键连接,那么你可以这样做:

    <changeSet id="foo" author="bar">
        <preConditions onFail="MARK_RAN">
            <and>
                <columnExists tableName="order" columnName="status"/>
                <columnExists tableName="order_history" columnName="status"/>
            </and>
        </preConditions>
        <comment>Update order_history.status with values from order.status, where order.id = order_history.order_id</comment>
        <update tableName="order_history">
            <column name="status" valueComputed="SELECT o.status FROM order o WHERE o.id = order_id"/>
        </update>
    </changeSet>
    

    如果这些表没有连接,那么您可以在添加新列时使用defaultValueComputed

        <addColumn tableName="order_history">
            <column name="status" type="varchar(255)" defaultValueComputed="some SQL query here"/>
        </addColumn>
    

    【讨论】:

    • 谢谢@htshame。顺便说一句,我认为第一个代码 sn-p 的外键关系没有任何必要工作。如果我错了,请纠正我,让我知道第一个代码 sn-p 是如何限制在具有外键关系的表中的。
    • 你说得对,只是为了保持一致性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2021-12-08
    • 2015-12-28
    • 1970-01-01
    • 2021-03-15
    • 2021-07-30
    相关资源
    最近更新 更多