BlueHotDog / sails-migrations

The missing, migrations, arm of the octopus
MIT License
158 stars 37 forks source link

table.timestamps() creates unusable column names #59

Open itsthatguy opened 9 years ago

itsthatguy commented 9 years ago

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');

2) 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 commented 9 years ago

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

BlueHotDog commented 9 years ago

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

itsthatguy commented 9 years ago

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

BlueHotDog commented 9 years ago

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