GN.
GN.

Reputation: 9869

Knex migration timestamp not timestamping

exports.up = async function(knex) {
  knex.schema.alterTable('posts', (t) => {
    t.timestamps(true, true)
  })
}

exports.down = async function(knex) {
  knex.schema.alterTable('posts', (t) => {
    t.dropTimestamps()
  })
}

According to the docs, this will create a created_at and updated_at columns.

But it does not.

How do you use Knex to create these columns.

Upvotes: 0

Views: 314

Answers (2)

Mikael Lepistö
Mikael Lepistö

Reputation: 19728

You are not executing your schema builder. Add await or return your schema builder from migration handler.

exports.up = async function(knex) {
  return knex.schema.alterTable('posts', (t) => {
    t.timestamps(true, true)
  })
}

exports.down = async function(knex) {
  return knex.schema.alterTable('posts', (t) => {
    t.dropTimestamps()
  })
}

Edit after @valem's comment

Code above is pretty much equivalent this, if async / await is not used:

exports.up = function(knex) {
  return Promise.resolve(knex.schema.alterTable('posts', (t) => {
    t.timestamps(true, true);
  }));
}

exports.down = function(knex) {
  return Promise.resolve(knex.schema.alterTable('posts', (t) => {
    t.dropTimestamps();
  }));
}

Upvotes: 1

valem
valem

Reputation: 1890

Try this (works for me)

t.dropColumn('created_at');
t.dropColumn('updated_at');

instead of t.dropTimestamps();

Upvotes: 0

Related Questions