You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Liquibase属性值替换失效求助:SQL文件${}语法报错

解决Liquibase SQL文件中属性替换失效的问题

问题分析

报错syntax error at or near "$"说明Liquibase没有对SQL文件中的${identity}占位符进行替换,直接将原始SQL发送给了数据库,导致数据库无法识别该语法。

解决方案

1. 开启属性替换功能

在根变更日志文件changelog-root.xml的<databaseChangeLog>标签中添加propertySubstitutionEnabled="true"属性,确保Liquibase解析包含的SQL文件时启用属性替换:

<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-latest.xsd"
                   propertySubstitutionEnabled="true">

    <property name="identity" value="bigserial" dbms="postgresql"/>
    <property name="identity" value="identity" dbms="h2"/>
    ...
    <include file="changelog-create-schema.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>

2. 验证属性匹配规则

确保当前连接的数据库与<property>标签中dbms属性的值匹配:

  • 连接PostgreSQL时,identity应被替换为bigserial
  • 连接H2时,identity应被替换为identity
    如果数据库类型不匹配,属性不会被正确替换,也会导致占位符残留。

3. 替代方案:使用<sql>标签分数据库编写SQL

如果上述方法仍不生效,可以放弃外部SQL文件,直接在变更日志中用<sql>标签按数据库类型编写SQL,避免属性替换问题:

<changeSet id="create_user_token_table" author="your-author">
    <sql dbms="postgresql">
        CREATE TABLE user_token (
            id bigserial primary key,
            token_encrypted varchar(200) NOT NULL UNIQUE,
            expiration timestamp NOT NULL
        );
    </sql>
    <sql dbms="h2">
        CREATE TABLE user_token (
            id identity primary key,
            token_encrypted varchar(200) NOT NULL UNIQUE,
            expiration timestamp NOT NULL
        );
    </sql>
</changeSet>

关键说明

Liquibase默认不会自动解析外部SQL文件中的属性占位符,必须显式开启propertySubstitutionEnabled才能触发替换逻辑。如果开启后仍有问题,检查变更日志的语法是否正确,以及数据库连接的类型是否与定义的dbms属性匹配。

内容的提问来源于stack exchange,提问作者freestar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 14:36:16