Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: drizzle-kit generate ignores index operators #2935

Closed
NehrDani opened this issue Sep 9, 2024 · 4 comments
Closed

[BUG]: drizzle-kit generate ignores index operators #2935

NehrDani opened this issue Sep 9, 2024 · 4 comments
Assignees
Labels
bug Something isn't working db/postgres drizzle/kit priority Will be worked on next

Comments

@NehrDani
Copy link

NehrDani commented Sep 9, 2024

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.2

Describe the Bug

Creating an index with an operator results in the wrong migration output.

export const users = pgTable(
  'users',
  {
    id: uuid('id').primaryKey().defaultRandom(),
    name: text('name').notNull(),
  },
  (table) => ({
    nameIdx: index().using('gin', table.name.op('gin_trgm_ops')),
  })
)

Using drizzle-kit generate the generated migration file does not include this:

CREATE INDEX IF NOT EXISTS "users_name_index" ON "users" USING gin ("name");

drizzle-kit push seems to not have this issue. because I get not errors there when trying to push my schema with this index.

Expected behavior

drizzle-kit generate creates the correct index with option.

CREATE INDEX IF NOT EXISTS "users_name_index" ON "users" USING gin ("name" gin_trgm_ops);

Environment & setup

No response

@NehrDani NehrDani added the bug Something isn't working label Sep 9, 2024
@pavelsvitek
Copy link

Maybe this will help. I have a column metadata: jsonb() and I needed to create index for specific field email

idx_metadata_email_trgm: index('idx_metadata_email_trgm').using(
      'gin',
      sql.raw("(metadata->>'email') gin_trgm_ops"),
    ),

I think you will be able to adapt your syntax using sql.raw(...) syntax

@L-Mario564 L-Mario564 self-assigned this Oct 24, 2024
@AndriiSherman
Copy link
Member

Was fixed in [email protected]

@diego-lipinski-de-castro

Same problem "drizzle-orm": "^0.38.3"

export const chats = pgTable('chats', {
  id: uuid('id').defaultRandom().primaryKey(),
  chatId: varchar('chat_id', { length: 256 }).notNull(),
  contactId: uuid('contact_id').references(() => contacts.id, { onDelete: 'cascade' }),
  whatsappId: uuid('whatsapp_id').references(() => whatsapps.id, { onDelete: 'cascade' }).notNull(),
  name: varchar('name', { length: 256 }).notNull(),
  customName: varchar('custom_name', { length: 256 }),
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
  deletedAt: timestamp('deleted_at'),
}, (table) => [{
  chat_whatsapp_idx: uniqueIndex('chat_whatsapp_idx').on(table.chatId, table.whatsappId),
}]);

export type Chat = typeof chats.$inferSelect;
export type InsertChat = typeof chats.$inferInsert;

generated

CREATE TABLE "chats" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"chat_id" varchar(256) NOT NULL,
"contact_id" uuid,
"whatsapp_id" uuid NOT NULL,
"name" varchar(256) NOT NULL,
"custom_name" varchar(256),
"created_at" timestamp DEFAULT now() NOT NULL,
"updated_at" timestamp DEFAULT now() NOT NULL,
"deleted_at" timestamp
);
--> statement-breakpoint

ALTER TABLE "chats" ADD CONSTRAINT "chats_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "chats" ADD CONSTRAINT "chats_whatsapp_id_whatsapps_id_fk" FOREIGN KEY ("whatsapp_id") REFERENCES "public"."whatsapps"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint

@diego-lipinski-de-castro

Nevermind, I coded wrong syntax, this is the correct syntax for indexes

export const chats = pgTable('chats', {
  id: uuid('id').defaultRandom().primaryKey(),
  chatId: varchar('chat_id', { length: 256 }).notNull(),
  contactId: uuid('contact_id').references(() => contacts.id, { onDelete: 'cascade' }),
  whatsappId: uuid('whatsapp_id').references(() => whatsapps.id, { onDelete: 'cascade' }).notNull(),
  name: varchar('name', { length: 256 }).notNull(),
  customName: varchar('custom_name', { length: 256 }),
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
  deletedAt: timestamp('deleted_at'),
}, (table) => [
  uniqueIndex('chat_whatsapp_idx').on(table.chatId, table.whatsappId),
]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working db/postgres drizzle/kit priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

5 participants