Liquibase not adding unique constraint

Viewed 96

hi i have a problem when adding unique constraint creation to my change log. in logs it shows the liquibase is reading change sets but i still cant find the unique constraint through query . the problem is it is only applied when i recreate database it doesn't work if table exists before.

<changeSet id="20220524-1" author="Saba Mosleh">
        <preConditions onFail="MARK_RAN">
            <sqlCheck expectedResult="0">
                SELECT DISTINCT count(CONSTRAINT_NAME) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE
                    TABLE_NAME = 'voucher_codes' AND CONSTRAINT_TYPE = 'UNIQUE' and CONSTRAINT_NAME = 'unique_voucher_code';
            </sqlCheck>
        </preConditions>
        <addUniqueConstraint tableName="voucher_codes" columnNames="code" constraintName="unique_voucher_code"/>
    </changeSet
1 Answers

a table DATABASECHANGELOG is created by liquibase delete your changelog record so liquibase will execute your change log when you restart the application, or simply add

<addUniqueConstraint 
tableName="voucher_codes" 
columnNames="code"
constraintName="unique_voucher_code" 
runAlways="true"/>

to execute the statement every time.

Related