在liquibase中,如何在外键列上使用从先前插入到另一个后续插入语句的另一个插入中生成的UUID

时间:2019-05-17 11:23:55

标签: spring-boot liquibase

我具有以下属性,并将插入语句插入我的liquibase变更集文件中 Block1

<property name="RESTRICTED_ADMIN_UUID" value="uuid_generate_v4()" dbms="postgresql"/>
<property name="FULL_ACCESS_ADMIN_UUID" value="uuid_generate_v4()" dbms="postgresql"/>
<property name="MANAGE_INBOX_C_UUID" value="uuid_generate_v4()" dbms="postgresql"/>

Block2

<insert tableName="role">
    <column name="id" valueComputed="${RESTRICTED_ADMIN_UUID}"/>
    <column name="version" value="0"/>
    <column name="description" value="Default restricted role for mapped-free-plan users"/>
    <column name="name" value="RESTRICTED_ADMIN"/>
    <column name="status" value="ACTIVE"/>
    <column name="bp_id" value="0"/> 
</insert>
<insert tableName="role">
    <column name="id" valueComputed="${FULL_ACCESS_ADMIN_UUID}"/>
    <column name="version" value="0"/>
    <column name="description" value="Default restricted role for mapped-free-plan users"/>
    <column name="name" value="FULL_ACCESS_ADMIN"/>
    <column name="status" value="ACTIVE"/>
    <column name="bp_id" value="0"/> <!--  0 means default template object -->
</insert>

Block3

<insert tableName="permissions"> 
    <column name="id" valueComputed="${MANAGE_INBOX_C_UUID}"/> 
    <column name="version" value="0"/> <column name="description" value="Manage inbox create"/> 
    <column name="name" value="MANAGE_INBOX_C"/> 
</insert>

Block4

<insert tableName="role_permission"> 
    <column name="id" valueComputed="uuid_generate_v4()"/> 
    <column name="version" value="0"/> 
    <column name="is_all" value="FALSE"/> 
    <column name="is_create" value="TRUE"/> 
    <column name="is_delete" value="FALSE"/> 
    <column name="is_read" value="FALSE"/> 
    <column name="is_update" value="FALSE"/> 
    <column name="permission_id" valueComputed="${MANAGE_INBOX_C_UUID}"/> 
    <column name="role_id" valueComputed="${RESTRICTED_ADMIN_UUID}"/>     
</insert>

<property name="RESTRICTED_ADMIN_UUID" value="uuid_generate_v4()" dbms="postgresql"/> <property name="FULL_ACCESS_ADMIN_UUID" value="uuid_generate_v4()" dbms="postgresql"/> <property name="MANAGE_INBOX_C_UUID" value="uuid_generate_v4()" dbms="postgresql"/>

1、2、3块正常运行,没有任何错误。

第4块出现以下错误

=======================================

原因:liquibase.exception.DatabaseException:错误:对表“ role_permission”进行插入或更新违反了外键约束“ fk2xn8qv4vw30i04xdxrpvn3bdi”-详细信息:键(permission_id)=(bfa5266c-7df1-48ca-8dae-44a4ab4cf83在“权限”表中。 [失败的SQL:INSERT INTO public.role_permission(id,版本,is_all,is_create,is_delete,is_read,is_update,Permission_id,role_id)VALUES(uuid_generate_v4(),'0','FALSE','TRUE','FALSE', 'FALSE','FALSE',uuid_generate_v4(),uuid_generate_v4())]

预期结果:

第4块中的insert语句类似于

<insert tableName="role">
    <column name="id" valueComputed="${RESTRICTED_ADMIN_UUID}"/>
    <column name="version" value="0"/>
    <column name="description" value="Default restricted role for mapped-free-plan users"/>
    <column name="name" value="RESTRICTED_ADMIN"/>
    <column name="status" value="ACTIVE"/>
    <column name="bp_id" value="0"/> 
</insert>
<insert tableName="role">
    <column name="id" valueComputed="${FULL_ACCESS_ADMIN_UUID}"/>
    <column name="version" value="0"/>
    <column name="description" value="Default restricted role for mapped-free-plan users"/>
    <column name="name" value="FULL_ACCESS_ADMIN"/>
    <column name="status" value="ACTIVE"/>
    <column name="bp_id" value="0"/> <!--  0 means default template object -->
</insert>

0 个答案:

没有答案