Skip to content

Instantly share code, notes, and snippets.

@jasheloper
Last active January 13, 2020 15:23
Show Gist options
  • Save jasheloper/dc55954cd0b3eb23bf73480308a7d113 to your computer and use it in GitHub Desktop.
Save jasheloper/dc55954cd0b3eb23bf73480308a7d113 to your computer and use it in GitHub Desktop.
knex migration sample - creating tables
exports.up = function(knex) {
return (knex.schema
.createTable('zoos', tbl => {
tbl.increments();
tbl.string('zoo_name', 128)
.notNullable()
.unique();
tbl.string('address', 128)
.notNullable()
.unique();
})
.createTable('species', tbl=> {
tbl.increments();
tbl.string('species_name', 128)
.notNullable()
.unique();
})
.createTable('animals', tbl=> {
tbl.increments();
tbl.string('animal_name', 128);
tbl.integer('species_id')
.unsigned()
.notNullable()
.references('id')
.inTable('species');
})
.createTable('zoo_animals', tbl => {
tbl.integer('animal_id')
.unsigned()
.notNullable()
.references('animals.id');
tbl.integer('zoo_id')
.unsigned()
.notNullable()
.references('zoos.id');
tbl.primary(['zoo_id', 'animal_id']);
})
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment