如何在Liquibase中创建具有外键约束的表?

时间:2016-09-30 14:04:25

标签: liquibase

我正在使用yaml,但我猜它几乎与xml或json相同。我发现你可以使用addForeignKeyConstraint,但我想在创建表时添加约束,而不是改变现有的表。我该怎么办?我可以这样做吗?

  - 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

1 个答案:

答案 0 :(得分:16)

我从未使用过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)