Skip to content

Commit

Permalink
docs: update predicate, useConstraint options in unique() (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Sep 1, 2022
1 parent 1b6847b commit 7a1fa2c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/guide/schema-builder.md
Expand Up @@ -723,19 +723,22 @@ If you want to chain primary() while creating new column you can use [primary](#

### unique

**table.unique(columns, options={[indexName: string], [deferrable:'not deferrable'|'immediate'|'deferred'], [storageEngineIndexType:'btree'|'hash'], [useConstraint:true|false]})**
**table.unique(columns, options={[indexName: string], [deferrable:'not deferrable'|'immediate'|'deferred'], [storageEngineIndexType:'btree'|'hash'], [useConstraint:true|false], [predicate: QueryBuilder]})**

Adds an unique index to a table over the given `columns`. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : [https://dev.mysql.com/doc/refman/8.0/en/create-index.html](https://dev.mysql.com/doc/refman/8.0/en/create-index.html). A default index name using the columns is used unless indexName is specified. If you need to create a composite index, pass an array of column to `columns`. Deferrable unique constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object. In MSSQL you can set the `useConstraint` option to true to create a unique constraint instead of a unique index.
Adds an unique index to a table over the given `columns`. In MySQL, the storage engine index type may be 'btree' or 'hash' index types, more info in Index Options section : [https://dev.mysql.com/doc/refman/8.0/en/create-index.html](https://dev.mysql.com/doc/refman/8.0/en/create-index.html). A default index name using the columns is used unless indexName is specified. If you need to create a composite index, pass an array of column to `columns`. Deferrable unique constraint are supported on Postgres and Oracle and can be set by passing deferrable option to options object. In MSSQL and Postgres, you can set the `useConstraint` option to true to create a unique constraint instead of a unique index (defaults to false for MSSQL, true for Postgres without `predicate`, false for Postgres with `predicate`). In PostgreSQL, SQLite and MSSQL a partial unique index can be specified by setting a 'where' predicate.

```js
knex.schema.alterTable('users', function(t) {
t.unique('email')
})
knex.schema.alterTable('job', function(t) {
t.unique(['account_id', 'program_id'], {indexName: 'users_composite_index', deferrable:'deferred', storageEngineIndexType: 'hash'})
t.unique(['account_id', 'program_id'], {indexName: 'job_composite_index', deferrable: 'deferred', storageEngineIndexType: 'hash'})
})
knex.schema.alterTable('job', function(t) {
t.unique(['account_id', 'program_id'], {indexName: 'users_composite_index', useConstraint:true})
t.unique(['account_id', 'program_id'], {indexName: 'job_composite_index', useConstraint: true})
})
knex.schema.alterTable('job', function(t) {
t.unique(['account_id', 'program_id'], {indexName: 'job_composite_index', predicate: knex.whereNotNull('account_id')})
})
```

Expand Down

0 comments on commit 7a1fa2c

Please sign in to comment.