Max Xie
Max Xie

Reputation: 3

Drizzle-ORM creating hash index on table

I want to create an index on a table in my Drizzle schema with one caveat, I want the index to be using the HASH index type. In the bottom you will find my current attempt.

However, when I am looking into the Drizzle documentation I see that there may or may not be support for this already, if the documentation is accurate, I would of course prefer to use its methods instead of writing custom SQL. Link to Drizzle documentation

Drizzle index documentation screenshot

The documentation states that only the name and on() params are currently supported. However, the example immediately below the statement says otherwise. Additionally, the typescript types mentioned in the example are defined in the codebase. And lastly, why does the using method take a SQL statement as the input and not a string (e.g. "b-tree", "hash" etc.)? Anyone from the Drizzle ORM community who could give me some guidance on this? Thanks.

export const template = pgTable(
  "post",
  {
    id: text("id")
      .primaryKey()
      .$defaultFn(() => crypto.randomUUID()),
    authorId: text("authorId")
      .notNull()
      .references(() => users.id, {onDelete: "cascade"}),
  },
  (table) => {
    return {
      authorIdIdx: sql`CREATE INDEX "authorIdIdx" ON "post" USING HASH ("authorIdId");`,
    };
  }
);

Upvotes: 0

Views: 848

Answers (1)

Yhozen
Yhozen

Reputation: 1317

Since 0.31 it is possible to have more complex indexes. See here drizzle docs

PD: It is completely safe to create indexes in this way, I think they were not familiar with drizzle.

Upvotes: 0

Related Questions