Adds a column to the index.
Also see columns for adding multiple columns at once.
import { sql } from 'kysely'
await db.schema
.createIndex('person_first_name_and_age_index')
.on('person')
.column('first_name')
.column<'last_name'>(sql`left(lower("last_name"), 1)`)
.column('age desc')
.where('last_name', 'is not', null)
.execute()
The generated SQL (PostgreSQL):
create index "person_first_name_and_age_index"
on "person" ("first_name", left(lower("last_name"), 1), "age" desc)
where "last_name" is not null
Adds a column to the index.
Also see columns for adding multiple columns at once.
import { sql } from 'kysely'
await db.schema
.createIndex('person_first_name_and_age_index')
.on('person')
.column('first_name')
.column<'last_name'>(sql`left(lower("last_name"), 1)`)
.column('age desc')
.where('last_name', 'is not', null)
.execute()
The generated SQL (PostgreSQL):
create index "person_first_name_and_age_index"
on "person" ("first_name", left(lower("last_name"), 1), "age" desc)
where "last_name" is not null
Adds a list of columns to the index.
Also see column for adding a single column.
import { sql } from 'kysely'
await db.schema
.createIndex('person_first_name_and_age_index')
.on('person')
.columns(['first_name', sql`left(lower("last_name"), 1)`, 'age desc'])
.execute()
The generated SQL (PostgreSQL):
create index "person_first_name_and_age_index"
on "person" ("first_name", left(lower("last_name"), 1), "age" desc)
Optionaloptions: AbortableQueryOptionsAdds an arbitrary expression as a column to the index.
import { sql } from 'kysely'
await db.schema
.createIndex('person_first_name_index')
.on('person')
.expression(sql`first_name COLLATE "fi_FI"`)
.column('gender')
.execute()
The generated SQL (PostgreSQL):
create index "person_first_name_index"
on "person" (first_name COLLATE "fi_FI", "gender")
Use column or columns with an Expression instead.
Adds the "if not exists" modifier.
If the index already exists, no error is thrown if this method has been called.
Adds nulls not distinct specifier to index.
This only works on some dialects like PostgreSQL.
db.schema.createIndex('person_first_name_index')
.on('person')
.column('first_name')
.nullsNotDistinct()
.execute()
The generated SQL (PostgreSQL):
create index "person_first_name_index"
on "test" ("first_name")
nulls not distinct;
Makes the index unique.
Specifies the index type.
await db.schema
.createIndex('person_first_name_index')
.on('person')
.column('first_name')
.using('hash')
.execute()
The generated SQL (MySQL):
create index `person_first_name_index` on `person` (`first_name`) using hash
Specifies the index type.
await db.schema
.createIndex('person_first_name_index')
.on('person')
.column('first_name')
.using('hash')
.execute()
The generated SQL (MySQL):
create index `person_first_name_index` on `person` (`first_name`) using hash
Adds a where clause to the query. This Effectively turns the index partial.
This is only supported by some dialects like PostgreSQL and MS SQL Server.
import { sql } from 'kysely'
await db.schema
.createIndex('orders_unbilled_index')
.on('orders')
.column('order_nr')
.where(sql.ref('billed'), 'is not', true)
.where('order_nr', 'like', '123%')
The generated SQL (PostgreSQL):
create index "orders_unbilled_index" on "orders" ("order_nr") where "billed" is not true and "order_nr" like '123%'
Column names specified in column or columns are known at compile-time and can be referred to in the current query and context.
Sometimes you may want to refer to columns that exist in the table but are not part of the current index. In that case you can refer to them using sql expressions.
Parameters are always sent as literals due to database restrictions.
Adds a where clause to the query. This Effectively turns the index partial.
This is only supported by some dialects like PostgreSQL and MS SQL Server.
import { sql } from 'kysely'
await db.schema
.createIndex('orders_unbilled_index')
.on('orders')
.column('order_nr')
.where(sql.ref('billed'), 'is not', true)
.where('order_nr', 'like', '123%')
The generated SQL (PostgreSQL):
create index "orders_unbilled_index" on "orders" ("order_nr") where "billed" is not true and "order_nr" like '123%'
Column names specified in column or columns are known at compile-time and can be referred to in the current query and context.
Sometimes you may want to refer to columns that exist in the table but are not part of the current index. In that case you can refer to them using sql expressions.
Parameters are always sent as literals due to database restrictions.
Adds a where clause to the query. This Effectively turns the index partial.
This is only supported by some dialects like PostgreSQL and MS SQL Server.
import { sql } from 'kysely'
await db.schema
.createIndex('orders_unbilled_index')
.on('orders')
.column('order_nr')
.where(sql.ref('billed'), 'is not', true)
.where('order_nr', 'like', '123%')
The generated SQL (PostgreSQL):
create index "orders_unbilled_index" on "orders" ("order_nr") where "billed" is not true and "order_nr" like '123%'
Column names specified in column or columns are known at compile-time and can be referred to in the current query and context.
Sometimes you may want to refer to columns that exist in the table but are not part of the current index. In that case you can refer to them using sql expressions.
Parameters are always sent as literals due to database restrictions.
Simply calls the provided function passing
thisas the only argument.$callreturns what the provided function returns.