knex icon indicating copy to clipboard operation
knex copied to clipboard

Feature request: partial index inside unique method

Open hanstf opened this issue 3 years ago • 2 comments

Environment

Knex version: 0.95.14 Database + version: postgres 12.5 OS: mac

Feature discussion / request

I saw that there is this feature request created #2525 but was closed. I have also checked that this PR #2401 add predicate options only for .index method. As postgres supports the unique partial index, is it possible to add the predicate inside the .unique method too?

hanstf avatar Dec 02 '21 09:12 hanstf

For the time being I have this working using this syntax:

  await knex.schema.raw(
    `CREATE UNIQUE INDEX "unique_partial_idx_userId_deleted" ON "users" ("userId", "deleted") WHERE "deleted" IS NOT TRUE`
  );

I would prefer something like this would work, but it throws an error

    table.index(['userId', 'deleted'], 'idx_userId_deleted', {
      indexType: 'UNIQUE',
      storageEngineIndexType: 'hash', 
      predicate: knex.whereNot('deleted', true)
    });

enchorb avatar Feb 28 '22 01:02 enchorb

We have a desire for this too. We have to use knex.raw, and to make a partial index requires basically reimplementing the where functions that knex already has ourselves.

mpcarolin avatar Aug 04 '22 22:08 mpcarolin