首页 > 解决方案 > Liquibase的where标签中的两个条件

问题描述

我想在引发 liquibase 和异常标记的位置添加两个条件:

Reason: liquibase.exception.DatabaseException: Unknown column 'uid' in 'where clause' [Failed SQL: UPDATE kw.media_file SET created = '1586352959' WHERE id=1 AND uid='admin']
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

这是更改日志:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-2.0.xsd"
      logicalFilePath="src/main/resources/db/update.xml">

<changeSet author="author" id="20200409-update">
    <update tableName="media_file">
        <column name="last_modified" type="int(11)" value="1586438425"/>
        <where>id=1 AND uid='admin'</where>
    </update>
</changeSet>

等效的sql语句在这里:

update person set created = 1586352959 where id = 2 and uid='admin';

当我运行 SQL 语句时,列已更新,但 liquibase 没有运气。感谢。

标签: sqldatabaseliquibase

解决方案


如评论中所述 - 您提供的错误和 changeSet 不匹配。

错误是uid在表中找不到该列media_file

您的 changeSet 综合正确,您可以在一个<where>标签中提供多个条件。

根据您提供的 SQL 语句

更新人员集 created = 1586352959 其中 id = 2 和 uid='admin';

所以我可以假设你正在寻找的 changeSet 可能看起来像:

<changeSet author="foo" id="bar">
    <update tableName="person">
        <column name="created" type="int(11)" value="1586438425"/>
        <where>id = 2 AND uid = 'admin'</where>
    </update>
</changeSet>

推荐阅读