I have the following Knex migration (using Postgresql):
import { Knex } from 'knex';
export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable('cars', (table: any) => {
table.string('id', 25).primary();
table.jsonb('car').notNullable().unique(["car->>'year'","car->>'model'","car->>'color'"], {indexName: 'unique_cars'});
});
}
export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTableIfExists('cars');
}
When I run the migration I get a unique index BUT it is based off of car
and not the fields I specified:
SELECT * FROM pg_indexes WHERE tablename = 'cars';
schemaname | tablename | indexname | tablespace | indexdef
------------+------------+-----------------------------+------------+----------------------------------------------------------------------------------------------
public | cars | cars_pkey | | CREATE UNIQUE INDEX cars ON public.cars USING btree (id)
public | cars | cars_car_unique | | CREATE UNIQUE INDEX cars_car_unique ON public.cars USING btree (car)
(2 rows)