Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify column datatype in Knex migration script

Tags:

knex.js

I am trying to modify the column data-type from date to timestamp in migration script using below code

knex.schema.alterTable('user', function(t) {
  t.timestamp('bifthday_date').alter(); 
});

Error : Knex:warning - migrations failed with error: table.timestamp(...).alter is not a function

like image 523
Asmita Avatar asked Mar 02 '17 08:03

Asmita


3 Answers

In the year's since this was first answered, it appears that Knex.js 0.21.15 (possibly earlier but don't know from when) will handle an alter column from date to timestamp, this is the code that I used:

exports.up = async function(knex, Promise) {
    await knex.schema.alterTable('campaigns', function(table) {
        table.timestamp('intake_start_date').alter();
        table.timestamp('intake_end_date').alter();
    });
}

Note that in the knex alter documentation:

Marks the column as an alter / modify, instead of the default add. Note: This only works in .alterTable() and is not supported by SQlite or Amazon Redshift.

like image 153
icc97 Avatar answered Jan 03 '23 19:01

icc97


I have managed to modify data-type using knex raw query.

knex.raw('alter TABLE user ALTER COLUMN birthday_date TYPE timestamp with time zone');
like image 27
Asmita Avatar answered Jan 03 '23 18:01

Asmita


I would assume that this failed because you tried to call a column by the name of "bifthday_date" as opposed to "birthday_date", which you used successfully later in your own answer.

like image 35
floodlitworld Avatar answered Jan 03 '23 19:01

floodlitworld