如何在 Liquibase 中创建带有外键约束的 table?
How to create a table with foreign key constraint in Liquibase?
我正在使用yaml,但我猜它与xml或json几乎相同。我发现您可以使用 addForeignKeyConstraint,但我想在 table 创建时添加约束,而不是改变现有的 table。我该怎么做?我可以做这样的事情吗?
- changeSet:
id: create_questions
author: Author
changes:
- createTable:
tableName: questions
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: user_id
type: int
constraints:
foreignKey:
referencedColumnNames: id
referencedTableName: users
nullable: false
- column:
name: question
type: varchar(255)
constraints:
nullable: false
我从未使用过 YAML 格式,但在 XML 更新日志中你可以这样做:
<column name="user_id" type="int">
<constraints nullable="false"
foreignKeyName="fk_questions_author"
references="users(id)"/>
</column>
等效的 YAML 应该是这样的:
- column:
name: user_id
type: int
constraints:
nullable: false
foreignKeyName: fk_questions_author
references: users(id)
我正在使用yaml,但我猜它与xml或json几乎相同。我发现您可以使用 addForeignKeyConstraint,但我想在 table 创建时添加约束,而不是改变现有的 table。我该怎么做?我可以做这样的事情吗?
- changeSet:
id: create_questions
author: Author
changes:
- createTable:
tableName: questions
columns:
- column:
name: id
type: int
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: user_id
type: int
constraints:
foreignKey:
referencedColumnNames: id
referencedTableName: users
nullable: false
- column:
name: question
type: varchar(255)
constraints:
nullable: false
我从未使用过 YAML 格式,但在 XML 更新日志中你可以这样做:
<column name="user_id" type="int">
<constraints nullable="false"
foreignKeyName="fk_questions_author"
references="users(id)"/>
</column>
等效的 YAML 应该是这样的:
- column:
name: user_id
type: int
constraints:
nullable: false
foreignKeyName: fk_questions_author
references: users(id)