Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate primary key value for each row in loadData file

Tags:

liquibase

How can I generate id for primary key autoincrement column when populating table with Liquibase? With my current configuration Liquibase is putting NULL into ID column.

My changelog file:

<?xml version="1.0" encoding="utf-8"?>
<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.2.xsd">

    <property name="now" value="now()" dbms="mysql,h2"/>
    <property name="now" value="current_timestamp" dbms="postgresql"/>

    <!--
        Added the entity Skill.
    -->
    <changeSet id="20141029084149" author="jhipster">
        <createTable tableName="T_SKILL">
            <column name="id" type="bigint">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="value" type="varchar(255)"/>
            <column name="description" type="varchar(255)"/>
        </createTable>

        <loadData encoding="UTF-8"
            file="config/liquibase/skills.csv"
            separator="|"
            tableName="T_SKILL"/>
    </changeSet>
</databaseChangeLog>

and skills.csv file:

value|description
java|Java
java-ee|Java Enterprise Edition
junit|JUnit
like image 312
delor Avatar asked Oct 29 '14 11:10

delor


1 Answers

You need to include autoIncrement="true" in the createTable column tag

<changeSet id="20141029084149" author="jhipster">
    <createTable tableName="T_SKILL">
        <column name="id" type="bigint" autoIncrement="true">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="value" type="varchar(255)"/>
        <column name="description" type="varchar(255)"/>
    </createTable>
</changeSet>
like image 112
Nathan Voxland Avatar answered Jan 02 '23 13:01

Nathan Voxland