How can I configure foreign keys through column tag attributes foreignKeyName
and references
? The only example I've found demonstrates how to add foreign keys after the fact.
4 Answers
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>
-
5Any idea how to do this in YAML? I'm afraid the Liquibase documentation always leaves me wanting, lost, or both.– MikeBCommented Mar 13, 2015 at 20:37
-
7Any idea where the
references="employee(id)"
syntax is documented?references
is defined as "Foreign key definition" but google leads most queries toaddForeignKeyConstraint
, which isn't the nicest way to go when you're creating a table or adding columns and already have<column>
tags.– LambartCommented Jul 3, 2015 at 5:51 -
9Using the syntax above didn't work for me, it generated the following
REFERENCES null(null))
. Instead ofreferences="employee(id)"
I had to usereferencedTableName="employee" referencedColumnNames="id"
– caspianCommented Jul 14, 2015 at 15:39 -
1Either references or referencedTablename/referencedColumnNames should work. The references attribute was the older version that was just passed as is into the create table statement, referencedTableName etc. is the newer version that is used to generate the string instead. Commented Jul 28, 2015 at 17:49
-
4Take a look at the latest XSD to ensure you're using the correct syntax. You can find it inside the liquibase-core-(version).jar under: liquibase/parser/core/xml/dbchangelog-(version).xsd. One advantage of using XML over YAML/JSON is that with the right XML namespace and IDE you can get auto-completion of available properties. Very helpful. Commented Aug 11, 2016 at 5:56
you have to add foreign constraint like:
- 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)
-
1I'd prefer using referencedTableName and referencedColumnNames instead. But this should be OK too– jbuddyCommented May 5, 2022 at 8:31
Maybe you can add foreign key as below:
<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>
For unidirectional relation, for cascade delete if you're using liquibase:
<addForeignKeyConstraint baseTableName="installment" baseColumnNames="account_id"
constraintName="fk_account_installment"
referencedTableName="account" referencedColumnNames="id"
onDelete="CASCADE"/>
And on Entity:
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "account_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Account account;
I'm still not sure why I needed @OnDelete on Entity which was needed to pass service level test although it was already specified in liquibase script. And If I remove cascade delete from liquibase, my integration test would fail. So I needed both. May be an experienced person can answer why declaration of cascade delete at both places was needed.
PS: If it helps, service level test are @SpringBootTest, and Integration Test are Cucumber tests with testcontainers.