简体   繁体   中英

How to set multiple foreign keys in the liquibase changesets?

I have a join table of two tables out of which one has id as primary key but the other has 3 columns as primary key basically composite primary key. Now, on the join table which column I should refer or more precisely how to refer multiple columns as foreign key while writing the changesets.

Here are the tables:

<changeSet id="Create X table">
 <createTable tableName="X">
  <column name="SERVICE" type="VARCHAR(50)">
    <constraints primaryKey="true"/>
  </column>
  <column name="ACTION" type="VARCHAR(50)">
    <constraints primaryKey="true"/>
  </column>
  <column name="TARGET" type="VARCHAR(50)">
    <constraints primaryKey="true"/>
  </column>
 </createTable>
</changeSet>

Second table:

<changeSet id="Create Y table">
<createTable tableName="Y">
  <column name="ID" type="VARCHAR(36)">
    <constraints primaryKey="true" primaryKeyName="XPKY"/>
  </column>
  <column name="NAME" type="VARCHAR(50)">
    <constraints nullable="false" unique="true"/>
  </column>
</createTable>

Joined table:

 <changeSet id ="Create X_Y_REL table">
<createTable tableName="X_Y_REL">
  <column name="Y_NAME" type="VARCHAR(50)">
    <constraints nullable="false" referencedTableName="Y" referencedColumnNames="NAME"
                 foreignKeyName="XFK1X_Y_REL"/>
  </column>
  <column name="X_ID" type="VARCHAR(150)">
    <constraints nullable="false" referencedTableName="X" referencedColumnNames="?????"
                 foreignKeyName="XFK2_X_Y_REL"/>
  </column>
</createTable>

So, I don't know what to put as the referenced column name here as there's no single primary key column in table X. It' primary key is composite key.

Any suggestion??

This is more of a database design question than a Liquibase question, but here are my thoughts on that.

You should probably just add a single integer or UUID column to each table to use as a surrogate primary key. In the long run, that will make things easier for you and anyone who comes after you. Then always use that key as the foreign key relation.

If you also want to have the database enforce that certain combinations of columns must also be unique for every row of the database, you can do that as well, but that is a separate concern.

If you don't want to add the surrogate primary key, then in your third changeset, you can reference the multiple column names that make up the composite key, separating them with commas. It would look like this:

<changeSet id ="Create X_Y_REL table" author="Steve">
   <createTable tableName="X_Y_REL">
     <column name="Y_NAME" type="VARCHAR(50)">
       <constraints nullable="false" referencedTableName="Y" referencedColumnNames="NAME"
                    foreignKeyName="XFK1X_Y_REL"/>
     </column>
     <column name="X_ID" type="VARCHAR(150)">
       <constraints nullable="false" referencedTableName="X" referencedColumnNames="SERVICE,ACTION,TARGET"
                    foreignKeyName="XFK2_X_Y_REL"/>
     </column>
  </createTable>
</changeSet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM