Miguel Ferreira
Miguel Ferreira

Reputation: 1417

How to insert a value if not duplicated with liquibase?

I need to convert a MySQL INSERT IGNORE INTO ... statement to a changeSet in liquibase and I prefer not to insert the statement directly in the changeSet.

Since I cannot find a direct translation, I'm wondering how to write a changeSet to insert a row only if the primary key part doesn't already exists in another row of the same table?

Upvotes: 3

Views: 4308

Answers (2)

Nathan Voxland
Nathan Voxland

Reputation: 15773

Liquibase doesn't have direct support for INSERT IGNORE support. If you are not wanting to use the <sql> tag, your only option is to use something like

<changeSet>
        <insert ...></insert>
        <modifySql>
             <replace replace="INSERT" with="INSERT IGNORE"/>
         </modifySql>
</changeSet>

Upvotes: 7

foxpaps
foxpaps

Reputation: 41

With a liquibase 3.2 and oracle12

<changeSet>
        <insert ...></insert>
        <modifySql>
             <replace replace="INSERT" with="insert /*+ ignore_row_on_dupkey_index(my_table, my_table_pk) */"/>
         </modifySql>
</changeSet>

remarks : Not use "replace value" but "replace replace"

It works too with the changeset loadData

Upvotes: 1

Related Questions