drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.56k stars 579 forks source link

[BUG]: Postgres drizzle-kit: Error while pulling indexes from a table with json/jsonb deep field index #2744

Open nowifi4u opened 2 months ago

nowifi4u commented 2 months ago

What version of drizzle-orm are you using?

0.32.1

What version of drizzle-kit are you using?

0.23.1

Describe the Bug

Error while pulling indexes. Tested using pg and postgres

Using 'pg' driver for database querying
[⣟] Pulling schema from database...
<project dir>\node_modules\drizzle-kit\bin.cjs:75970
        Error.captureStackTrace(err2);
              ^

error: malformed array literal: "{(data #>> '{field1}'::text[])}"
    at <project dir>\node_modules\drizzle-kit\bin.cjs:75970:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.query (<project dir>\node_modules\drizzle-kit\bin.cjs:117909:26)
    at async <project dir>\node_modules\drizzle-kit\bin.cjs:19071:31 {
  length: 144,
  severity: 'ERROR',
  code: '22P02',
  detail: 'Unexpected "{" character.',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'arrayfuncs.c',
  line: '567',
  routine: 'ArrayCount'
}

Steps to Reproduce the Problem

Utils:

export type PgExtraConfigJson = ExtraConfigColumn<ColumnBaseConfig<ColumnDataType, string>>;

export function extraJsonFieldDeep (column: PgExtraConfigJson, keys: Array<string | number>): SQL {
  return sql.raw(`("${column.name}" #>> '{${(keys as string[]).join(',')}}')`);
}

Schema:

export type JsonData = {
  field1: string | null;
  obj1: {
    field2?: string | null;
  };
};

export const testTable = pgTable('test', {
  id: uuid('id').notNull().primaryKey(),

  data: jsonb('data').notNull().$type<JsonData>(),
}, (table) => {
  return {
    idx_data_field1: index('idx__test__data_field1').on(extraJsonFieldDeep(table.data, ['field1'])),
  };
});
  1. run drizzle-kit generate && drizzle-kit push to create index
  2. run drizzle-kit generate && drizzle-kit push to try updating table

Expected behavior

drizzle-kit push should run normally

Environment & setup