Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate index on a column #1336

Open
alphanso opened this issue Apr 10, 2016 · 2 comments
Open

Duplicate index on a column #1336

alphanso opened this issue Apr 10, 2016 · 2 comments

Comments

@alphanso
Copy link

Hi,
I am using knex for my project.

exports.up = function(knex, Promise) {
  return knex.schema.createTable('users', function (t) {
    t.increments();

    // Database authenticatable
    t.string('email').notNullable().unique().index();
    t.string('encrypted_password').notNullable();

    // Recoverable
    t.string('reset_password_token').unique().index();
    t.dateTime('reset_password_sent_at');

    // Rememberable
    t.dateTime('remember_created_at');

    // Trackable
    t.integer('sign_in_count').notNullable().defaultTo(0);
    t.dateTime('current_sign_in_at');
    t.dateTime('last_sign_in_at');
    t.specificType('current_sign_in_ip', 'inet');
    t.specificType('last_sign_in_ip', 'inet');

    // Confirmable
    // t.string('confirmation_token').unique().index();
    // t.dateTime('confirmed_at');
    // t.dateTime('confirmation_sent_at');
    // t.string('unconfirmed_email');  // Only if using reconfirmable

    // Lockable
    // t.integer('failed_attempts').defaultTo(0).notNullable(); // Only if lock strategy is 'failed_attempts'
    // t.string('unlock_token').unique().index(); // Only if unlock strategy is 'email' or 'both'
    // t.dateTime('locked_at')

    t.timestamps();
  });
};

This migration creates total of 5 indexes.
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
"users_email_unique" UNIQUE CONSTRAINT, btree (email)
"users_reset_password_token_unique" UNIQUE CONSTRAINT, btree (reset_password_token)
"users_email_index" btree (email)
"users_reset_password_token_index" btree (reset_password_token)

@WhiteCat6142
Copy link

console.log(
knex.schema.createTable("x",function(table){
    table.string("name").unique().index();
})
.toString());

Result:
create table "x" ("name" varchar(255));
create unique index x_name_unique on "x" ("name");
create index x_name_index on "x" ("name")

Forget turn off the regular index?

@WhiteCat6142
Copy link

oh,I realize that unique() create unique index automatically.

console.log(
knex.schema.createTable("x",function(table){
    table.string("name").unique();
})
.toString());

Result:
create table "x" ("name" varchar(255));
create unique index x_name_unique on "x" ("name")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants