sails-migrations icon indicating copy to clipboard operation
sails-migrations copied to clipboard

table.timestamps() creates unusable column names

Open itsthatguy opened this issue 9 years ago • 4 comments

This is more a knex issue than sails-migrations issue, but wanted to have the issue documented for future users. http://knexjs.org/#Schema-timestamps

Issue:

table.timestamps() creates created_at and updated_at while sails is expecting createdAt and updatedAt

Resolutions:

  1. If you haven't yet used table.timestamps():
table.timestamp('createdAt');
table.timestamp('updatedAt');
  1. If you've already used table.timestamps():

Create a migration for fixing the erroneous columns (sails-migrations generate fix_timestamps)

'use strict';

exports.up = function(knex, Promise) {
  function renameUp(table) {
    table.renameColumn('created_at', 'createdAt')
    .renameColumn('updated_at', 'updatedAt');
  }

  Promise.all([
    knex.schema.table('myTable', renameUp)
  ]);
};

exports.down = function(knex, Promise) {
  function renameDown(table) {
    table.renameColumn('createdAt', 'created_at')
    .renameColumn('updatedAt', 'updated_at');
  }

  Promise.all([
    knex.schema.table('myTable', renameDown)
  ]);
};

itsthatguy avatar Mar 27 '15 17:03 itsthatguy

https://github.com/tgriesser/knex/issues/754

itsthatguy avatar Mar 27 '15 17:03 itsthatguy

nice catch.. i wonder if we can fix that in some way..

BlueHotDog avatar Mar 27 '15 17:03 BlueHotDog

Might be able to hook into our knex object and overwrite it.

itsthatguy avatar Mar 27 '15 19:03 itsthatguy

yeah, seems like there is no easy way to configure it.. hmm, we could patch that before it's passed to the function

BlueHotDog avatar Mar 29 '15 22:03 BlueHotDog