Java Liquibase:如何在列标签中设置外键约束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24718545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Liquibase: How to Set Foreign Key(s) Constraint in Column Tag?
提问by Ari
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.
如何配置通过列标签属性外键foreignKeyName
和references
?我发现的唯一示例演示了如何在事后添加外键。
回答by Nathan Voxland
Use a nested <constraints>
tag in your column tag.
<constraints>
在列标签中使用嵌套标签。
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>
回答by ozhanli
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>
回答by ankit
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)