Skip to content

Instantly share code, notes, and snippets.

@jasheloper
Last active January 13, 2020 15:24
Show Gist options
  • Save jasheloper/f4c1c1535751cb770cbc23ea247e84c3 to your computer and use it in GitHub Desktop.
Save jasheloper/f4c1c1535751cb770cbc23ea247e84c3 to your computer and use it in GitHub Desktop.
migration with foreign key sample
.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