Adding a Complex Unique Constraint to Liquibase

I am creating a link table that has 3 columns; id, product_id, tournament_id.

Adding a unique constant to the id column is trivial, but I want to make sure that any pair (product_id, tournament_id) is unique.

Liquibase.org example shows

<changeSet author="liquibase-docs" id="addUniqueConstraint-example"> <addUniqueConstraint catalogName="cat" columnNames="id, name" constraintName="const_name" deferrable="true" disabled="true" initiallyDeferred="true" schemaName="public" tableName="person" tablespace="A String"/> </changeSet> 

but is it possible to do this in a <createTable> block?

Also, just for confirmation; Does this create a composite unique constraint in two columns or create two separate unique constraints?

+16
unique-constraint liquibase
source share
2 answers

I am pretty sure that:

  • You cannot do this inside the createTable tag itself, but you can do it in the same set of changes as when creating the table.
  • creates a composite unique constraint for two columns. One way to check is to run linibase using the command to generate SQL to update, and not to run the update command and check what it does for your database. On the command line, instead of running liquibase update you run liquibase updateSQL .
+15
source share

You can read the Liquibase manual and a similar problem you can find here.

In your case, it should be:

 <changeSet author="liquibase-docs" id="addUniqueConstraint-example"> <addUniqueConstraint columnNames="id, product_id, tournament_id" constraintName="your_constraint_name" tableName="person" /> </changeSet> 
+2
source share

All Articles