Datatypes in migration file issues

Hello,
I am creating table using adonis migration but below are issues which I am facing,

  1. I am not able to add dataType as char in migration file of adonis.
  2. as it takes created_at & updated _at fields by-default but, when I hit update query then both values getting changed instead I want to change only updated_at field.
    Please help me to come out of these major issues.:frowning:

What is your database management system (DBMS) ? Knex.js does not offer char() type, however if your DBMS supports char type then Knex.js allows you to define new types (char in your context) using specifType().

as it takes created_at & updated _at fields by-default but, when I hit update query then both values getting changed instead I want to change only updated_at field.
Please help me to come out of these major issues.

In case you do not need the created_at field, he official documentation addresses your issue; add this to your model:

static get createdtedAtColumn () {
  return null
}

If you need this field however, then please share the code which you think leads to updating this column too.

1 Like

Thanks for quick reply :slight_smile:
after all searches , for now I have added alter command in that put query like,

this.raw("ALTER TABLE test_column"+
"ADD COLUMN updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER created_at"+
“ADD COLUMN status CHAR(50) NULL”);

Is it ok if I do to resolve above issue ? is it affect my other DB operations ?
& how to run only particular migration file in adonisjs ?

That alter operation you did to the table is necessary but not enough, you must also add the static function I shared previously into the related model.

Yes, you need to run the migrations again so that this alter operation takes effect.

1 Like

Is there anyway if I want to run specific migration file ?

1 Like

Good question but Adonis deals with that in a smart way, so you do not need to worry about it. I mean if you just run:

adonis migration:run

Adonis is smart enough to see which migration is pending and which one is already executed. It will run only the pending migrations (in your case this will be the migration file where you wrote the alter operation).

Yes I agree on your point but the issue is when I re-run the migration file it gives table already exist exception when I use create() method.
So, to avoid this error I tried createIfNotExists() but it gives below warning like,

BEGIN; trx6 +44ms
Use async .hasTable to check if table exists and then use plain .createTable. Since .createTableIfNotExists actually just generates plain “CREATE TABLE IF NOT EXIST…” query it will not work correctly if there are any alter table queries generated for columns afterwards. To not break old migrations this function is left untouched for now, but it should not be used when writing new code and it is removed from documentation.

So what need to do to avoid this issue ?

According to this error message you shared, I think you did not create the migration file where you wanted to alter the table correctly.

When you create a migration file with the name of that table, you will see your command line prompts you with an option to either create or alter the table. You have to select ‘Alter table’ option. From that, you will see a new migration file is created. Then you write carefully what you want to alter (and do not forget about the rollback code in that file)

You will write the alter code in the up() method as shown in the documentation:

up () {
  this.alter('users', (table) => {
    // add new columns or remove existing
  })
}

The down() method is the reverse of the up action (rollback).