Tuesday, 27 March 2018

Objection.js (or rather, knex schema builder)

So I've been using objection.js for a while, and I've read in their API:Create database schema. You should use knex migration files to do this.So I looked into the knex migrations and I have some issues. I've already posted this to their github, with lackluster response.tl;dr: I was using knex migrations for an app with this schema (give or take) representing my database:exports.up = function (knex, Promise) { return knex.schema .createTable('servers', function (table) { table.increments('id').primary(); table.string('serverName', 50); table.integer('fault_id').references('faults.id'); }) .createTable('faults', function (table) { table.increments('id').primary(); table.text('faultDescription', 'longtext'); }); }; exports.down = function (knex, Promise) { return knex.schema .dropTable('servers') .dropTable('faults'); }; I've been using this database for a while now and I have lots of data already in the database. Now I want to add another column to the servers table, using knex migrations.If I run knex migrate:rollback I've lost all the data. If I change the schema and run knex migrate:latest I get an error that it's already up to date. If I create a new schema with alterTable, I've lost reference to the schema above. I literally don't see a way of using schema migrate to solve this issue.What is the correct approach?

Submitted March 27, 2018 at 07:41PM by JavascriptFanboy

No comments:

Post a Comment