Knex 迁移时间戳不加盖时间戳


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()
  })
}

根据文档,这将创建一个created_atupdated_at列。

但事实并非如此。

如何使用 Knex 创建这些列。

您没有执行架构构建器。从迁移处理程序添加等待或返回架构生成器。

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()
  })
}

@valem评论后编辑

上面的代码几乎等效于这一点,如果不使用 async/await:

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();
  }));
}

试试这个(对我有用(

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

而不是 t.dropTimestamps((;

最新更新