web-dev-qa-db-fra.com

Liquibase: Comment définir une contrainte de clé étrangère dans une balise de colonne?

Comment configurer des clés étrangères via les attributs de balise de colonne foreignKeyName et references? Le seul exemple que j'ai trouvé montre comment ajouter des clés étrangères après coup .

34
Ari

Utilisez une balise imbriquée <constraints> dans votre balise de colonne.

Exemple:

<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>
43
Nathan Voxland

Peut-être que vous pouvez ajouter une clé étrangère comme ci-dessous:

<changeSet id="1" author="ozhanli">
    <!--
    Owner Entity.
    -->
    <createTable tableName="owner">
        <column name="id" type="bigint" autoIncrement="true">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="name" type="varchar(255)">
            <constraints nullable="true" />
        </column>
    </createTable>

    <!--
    Car Entity.
    -->
    <createTable tableName="car">
        <column name="id" type="bigint" autoIncrement="true">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="brand" type="varchar(255)">
            <constraints nullable="true" />
        </column>
        <column name="owner_id" type="bigint">
            <constraints nullable="true" />
        </column>
    </createTable>

    <!--
    Constraints for Car entity
    -->
    <addForeignKeyConstraint baseColumnNames="owner_id"
                             baseTableName="car"
                             constraintName="fk_car_owner_id"
                             referencedColumnNames="id"
                             referencedTableName="owner"/>
</changeSet>
1
ozhanli

vous devez ajouter une contrainte étrangère comme:

- changeSet:
    id: create_account_table
    author: ankit
    changes:
    - createTable:
        tableName: account
        columns:
        - column:
            name: accn_id
            type: uuid
            constraints:
              primaryKey: true
              primaryKeyName: pk_account
- changeSet:
    id: create_table_abc
    author: ankit
    changes:
    - createTable:
        tableName: abc
        columns:
        - column:
            name: id
            type: uuid
            constraints:
              primaryKey: true
              primaryKeyName: pk_abc
        - column:
            name: accn_id
            type: UUID
            constraints:
              nullable: false
              foreignKeyName: fk_abc_account
              references: account(accn_id)
1
ankit