Working with Universally Unique Identifiers

In earlier versions of Liquibase, Universally Unique Identifiers (UUIDs) may not have functioned correctly. As of version 3.6.0+, they are a supported data type. For more information on using UUIDs in table columns, see the column documentation.

How to use UUID and load data

  1. Define a table. For example, this changelog creates two tables: project and project_objectives. It then uses the loadData Change Type to load data into the project_objectives table:
  2. <?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"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xmlns:pro="http://www.liquibase.org/xml/ns/pro"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
            http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
            http://www.liquibase.org/xml/ns/dbchangelog-ext
            http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
            http://www.liquibase.org/xml/ns/pro
            http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
    	<changeSet id="1" author="mike">
    		<createTable tableName="project">
    			<column name="id" type="uuid">
    				<constraints primaryKey="true" nullable="false"/>
    			</column>
    		</createTable>
    	</changeSet>
    	<changeSet id="2" author="mike">
    		<createTable tableName="project_objectives">
    			<column name="objectives_id" type="uuid">
    				<constraints nullable="false"/>
    			</column>
    			<column name="project_id" type="uuid">
    				<constraints nullable="false"/>
    			</column>
    		</createTable>
    		<addPrimaryKey columnNames="project_id, objectives_id" tableName="project_objectives"/>
    	</changeSet>
    	<changeSet id="2-data" author="mike" context="test">
    		<loadData
    				file="test-data/project_objectives.csv"
    				separator=";"
    				tableName="project_objectives">
    			<column name="objectives_id" type="uuid"/>
    			<column name="project_id" type="uuid"/>
    		</loadData>
    	</changeSet>
    </databaseChangeLog>
  3. Add a CSV for Liquibase to insert (with the same name as the loadData file above. In this case, it is project_objectives.csv in the test-data folder):
  4. objectives_id;project_id			
    8d1208fc-f401-496c-9cb8-483fef121234;ea5bc0ab-6ccf-4791-a048-694a5be1d309
    8d1208fc-f401-496c-9cb8-483fef121234;1a8aebee-2061-4220-99ce-ddaf2b22d23f
    cce54793-00b8-4830-8f3c-e4cb97b8fb70;1bc59ddb-8d4d-41d0-9c9a-34e837de5678
    234c2416-e7f2-47bf-afdc-03645b3a98ab;1bc59ddb-8d4d-41d0-9c9a-34e837de5678
    234c2416-e7f2-47bf-afdc-03645b3a98ab;1482cc79-fd01-42e4-b5a1-a7f0db546d00
    12349c78-454a-4167-9a1e-8e853a4cf2f5;072e217f-76b3-4cae-b3bd-f7c71a8e0003
    1234bf02-0ffa-4b52-b22f-a10e76e31cb5;98f275cd-934d-42cd-91e4-fa0acd7dc5eb
    e6b920b7-4ac4-4b62-aea7-36f75e3ad610;dddd728e-fd2c-4a1c-8a10-ffd84ed1603c
    ef8614c9-f4d7-4b56-9aaa-088cbe9f7e71;dddd728e-fd2c-4a1c-8a10-ffd84ed1603c
    678abcde-898a-4cd9-8aae-99a1943b2ebf;dddd728e-fd2c-4a1c-8a10-ffd84ed1603c
  5. Run liquibase update to create the table and insert the data.

Actual behavior

  • Tables are created by Liquibase successfully (changesets 1 and 2)
  • Liquibase inserts the data into that table (project_objectives).
  • Liquibase also supports adding UUIDs to the database successfully.