Liquibase: How to Set Foreign Key(s) Constraint in Column Tag?

Use a nested <constraints> tag in your column tag.

Example:

<changeSet id="SAMPLE_1" author="alice">
    <createTable tableName="employee">
        <column name="id" type="int" autoIncrement="true">
            <constraints primaryKey="true"/>
        </column>
        <column name="first_name" type="varchar(255)"/>
        <column name="last_name" type="varchar(255)">
            <constraints nullable="false"/>
        </column>
    </createTable>
</changeSet>

<changeSet id="create address table" author="bob">
    <createTable tableName="address">
        <column name="id" type="int" autoIncrement="true">
            <constraints primaryKey="true"/>
        </column>
        <column name="line1" type="varchar(255)">
            <constraints nullable="false"/>
        </column>
        <column name="line2" type="varchar(255)"/>
        <column name="city" type="varchar(100)">
            <constraints nullable="false"/>
        </column>
        <column name="employee_id" type="int">
            <constraints nullable="false" foreignKeyName="fk_address_employee" references="employee(id)"/>
        </column>
    </createTable>
</changeSet>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)