Home > OS >  How to index timestamp type with knexjs
How to index timestamp type with knexjs

Time:12-10

I am trying to index created_at and updated_at columns with knex js But when I assign index(), it shows me this error:

Property 'index' does not exist on type 'void'

await knex.schema.createTable('ontario_user_reports', (table) => {
    table.string('user_id').notNullable().index();
    table.timestamps(false, true).index(); // here
    table.string('type').notNullable().index();
});

Can you propose me a correct way to index created_at?

CodePudding user response:

You should be able to add your index like this:

await knex.schema.createTable('ontario_user_reports', (table) => {
    table.string('user_id').notNullable().index();
    table.timestamps(false, true);
    table.string('type').notNullable().index();

    // Add this line:
    table.index('created_at');
});

Optionally, set a name and additional options using the parameters as described here: https://knexjs.org/#Schema-index

  • Related