首页 > 解决方案 > 我可以从 Liquibase LoadUpdateData XML 标记中跳过主键子句吗

问题描述

我的表结构如下

CREATE TABLE domain_stat (
    domain_stat bpchar(2) NOT NULL,
    des_domain_stat varchar(40) NULL,
    cde_domain_stat_catg bpchar(1) NOT NULL,
    date_crea timestamp NOT NULL,
    id_crea_user bpchar(8) NOT NULL,
    date_updt timestamp NOT NULL,
    id_updt_user bpchar(8) NOT NULL
);

因此,在这种情况下不涉及主键,甚至我的实际数据也确实包含重复的行。

问题是通过使用 liquibase 语法将数据加载到表中,我必须primarykey强制添加子句。有什么办法可以跳过主键。或添加复合键的任何替代方法,以便可以将完整行标识为唯一行。

<?xml version="1.1" 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-3.8.xsd">
    <changeSet author="liquibase-docs"
        id="loadUpdateData-example" context="!prod">
        <loadUpdateData encoding="UTF-8"
            file="config/liquibase/data/domain_stat.csv" onlyUpdate="false" primaryKey="id"
            quotchar="'" separator="," tableName="domain_stat">
        </loadUpdateData>
    </changeSet>
</databaseChangeLog>

标签: databaseprimary-keyliquibase

解决方案


我找到了解决这个问题的方法。因为我的桌子上没有主键。我创建了一个可执行bat文件,并使用 Postgres\COPY指令将 CSV 文件数据加载到相应的表中。

bat 文件为

set PGPASSWORD=root
set DB_HOST_NAME="localhost"
set DB_PORT="5434"
set DB_USER="postgres"
set DB_NAME="db_dev"

echo "Importing accessory Data"
psql -h %DB_HOST_NAME% -p %DB_PORT% -U %DB_USER% -d %DB_NAME% -c "\COPY "dev".p_table       FROM ../data/p_data.csv delimiter ',' csv HEADER NULL AS 'null';"

我的 liquibase XML 文件看起来像

<?xml version="1.1" 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-3.8.xsd">
    <changeSet author="liquibase-docs" context="!prod"
        id="executeCommand-example">
        <executeCommand executable="import-data.bat"  />
    </changeSet>
</databaseChangeLog>

然后将此文件导入主更改日志,这对我有用!


推荐阅读