Liquibase - insert rows with uuid

For MySQL, put your property just before changeSet tag:

    <property name="u_id" value="uuid()" dbms="mysql"/>

then

    <column name="ID" type="varchar(255)" valueComputed="${u_id}"/>

NOTE: here valueComputed is used, not defaultValueComputed.


Some databases supports UUID columns: Generate UUID values by default for each row on column of UUID type in H2 Database Engine

I don't think that Liquibase has embedded UUID generator, have a look at defaultValueComputed/valueComputed property for column (http://www.liquibase.org/documentation/column.html) + DB function to generate UUID


You can do this by using properties that are defined depending on the current DBMS.

<property name="uuid_type" value="uuid" dbms="postgresql"/>
<property name="uuid_type" value="uniqueidentifier" dbms="mssql"/>
<property name="uuid_type" value="RAW(16)" dbms="oracle"/>

<property name="uuid_function" value="uid.uuid_generate_v4()" dbms="postgresql"/>
<property name="uuid_function" value="NEWID()" dbms="mssql"/>
<property name="uuid_function" value="sys_guid()" dbms="oracle"/>

Then use those properties when defining the table:

<column name="id" type="${uuid_type}" defaultValueComputed="${uuid_function}">
    <constraints nullable="false" unique="true" />
</column>

Note that you need to use defaultValueComputed, not value

If the column is defined with a default value, just leave it out in your insert statements and the database will then generate the UUID when inserting.

Tags:

Sql

Liquibase