首页 > 解决方案 > 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 创建这些列。

标签: knex.js

解决方案


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


推荐阅读