How to set a default date using knex?
I'm using knex and I'm trying to set a defaut date that takes in consideration the current date and sum it of one day, so for example the default date for today should be:
23/04/2020
In my migration file I've added this:
exports.up = async (knex, Promise) => (await knex.schema.hasTable('access-code'))
? null
: knex.schema.createTable('access-code', function (table) {
table.increments('id')
table.date('expire_at').defaultTo([knex.fn.now(), 1])
});
but this return:
default character set utf8mb4 - ER_INVALID_DEFAULT: Invalid default value for 'expire_at'
Hi, everyone. Have the same issue, maybe someone has the solution?
Based on the documentation from Mysql https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html you probably need to set the Date string instead of timestamp.
I can't find anything in Knex documentation that could help here so I used the syntax below by using knex.raw.
table.date('expire_at').defaultTo(knex.raw('(CURRENT_DATE())'))
If you are using different adapter you might check if your database supports that function or has different one.
I am facing this same issue, i want to set an expire_at column, that sets a future date: This is the knex code: table.timestamp('expires_at').notNullable().defaultTo(knex.raw('DATE_ADD(?, INTERVAL ? DAY)', [knex.fn.now(), 1]));