I am using yaml, but I guess it is almost the same as xml or json. I found that you can use addForeignKeyConstraint, but I want to add the constraint at table creation, not altering an existing table. How should I do that? Can I do something like this?
- 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
To create a table for your database, follow these steps: Step 1: Add the createTable Change Type to your changeset with the needed attributes, as shown in the examples. Step 2: Deploy your changeset by running the update command. Now, you should see a new table.
NOT DEFERRABLE. This controls whether the constraint can be deferred. A constraint that is not deferrable will be checked immediately after every command. Checking of constraints that are deferrable may be postponed until the end of the transaction (using the SET CONSTRAINTS command).
A table can have multiple foreign keys based on the requirement.
I never used the YAML format, but in an XML changelog you can do this:
<column name="user_id" type="int">
<constraints nullable="false"
foreignKeyName="fk_questions_author"
references="users(id)"/>
</column>
The equivalent YAML should be something like this:
- column:
name: user_id
type: int
constraints:
nullable: false
foreignKeyName: fk_questions_author
references: users(id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With