Last active
January 13, 2020 15:24
-
-
Save jasheloper/f4c1c1535751cb770cbc23ea247e84c3 to your computer and use it in GitHub Desktop.
migration with foreign key sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.createTable('farm_animals', tbl => { | |
tbl.integer('farm_id') | |
.unsigned() | |
.notNullable() | |
.references('id') | |
// this table must exist already | |
.inTable('farms') | |
tbl.integer('animal_id') | |
.unsigned() | |
.notNullable() | |
.references('id') | |
// this table must exist already | |
.inTable('animals') | |
// the combination of the two keys becomes our primary key | |
// will enforce unique combinations of ids | |
tbl.primary(['farm_id', 'animal_id']); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment