如何创建不可为空的日期列?

How to create a date column not nullable?

我正在使用 sqlite。我需要创建一个 datenot nullable。我试过了,但没有用。

return knex.schema.table("my_table", (table) => {
  table.date("important_day").notNullable().defaultTo(knex.fn.now());
});

使用此代码,我收到此错误消息:

Unhandled rejection Error: SQLITE_ERROR: Cannot add a column with non-constant default. Knex:warning - migrations failed with error: alter table minisermao add column diaminisermao date not null default CURRENT_TIMESTAMP - SQLITE_ERROR: Cannot add a column with non-constant default

如何解决?

确实对于创建 table 我们有一个好的结果:

exports.up = function(knex, Promise) {
  return knex.schema.createTable("foo", function(tb){
    tb.date("xpto").notNullable().defaultTo(knex.fn.now());
  })
};

exports.down = function(knex, Promise) {  
  return knex.schema.dropTable("foo");
};

然而,对于更改 table 操作,它给我们一个错误。

如果可能,请尝试以下操作:

进行迁移以删除当前 table:

exports.up = function(knex, Promise) {
  return knex.schema.dropTable("foo");
};

exports.down = function(knex, Promise) { 
  return knex.schema.createTable("foo", function(tb){
    tb.date("xpto").notNullable().defaultTo(knex.fn.now());
  }); 
};

然后进行第二次迁移以再次创建 table。

我建议采用这种方法,因为如果项目安装在生产环境中,更改已执行的迁移将不是一种健康的方法。

希望对您有所帮助。

更新:

完全相同的改变 table 在 postgresql 上完美运行。