drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: postgresql migrations generation: the schema is ignored for indexes (indexes applied to "public" schema)

Open mppub opened this issue 2 years ago β€’ 3 comments
trafficstars

What version of drizzle-orm are you using?

0.23.2

Describe the Bug

Basically indexes are applied to public schema no matter if you use custom postgresql schema or not

Contents of my schema.ts looks like this:

const accountSchema = pgSchema('account');
export const usersTable = accountSchema.table("users", {
	id: uuid("id").defaultRandom().primaryKey(),
	email: varchar("email", { length: 255 }).notNull(),
	// ....
},
(table) => {
	return {
		emailIdx: index().on(table.email),
		emailUnique: uniqueIndex("users_email_unique").on(table.email),
		idUnique: uniqueIndex("users_id_unique").on(table.id),
	}
})

that generates this sql migration output:

CREATE TABLE IF NOT EXISTS "account"."users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"email" varchar(255) NOT NULL,
	// ...
);

CREATE INDEX IF NOT EXISTS users_email_index ON users ("email");
CREATE UNIQUE INDEX IF NOT EXISTS users_email_unique ON users ("email");
CREATE UNIQUE INDEX IF NOT EXISTS users_id_unique ON users ("id");

the indexes are created on public.user rather than account.users

If there is an API for this let me know, I will submit a docs update PR.

mppub avatar Apr 08 '23 15:04 mppub

Thanks! Yeah it’s a bug. Will fix it

AndriiSherman avatar Apr 08 '23 16:04 AndriiSherman

Related - https://github.com/drizzle-team/drizzle-orm/issues/440

I don't think drizzle-kit handles multiple schemas at all.

erickreutz avatar Apr 13 '23 13:04 erickreutz

@erickreutz it's right. Will be next to get fixed on drizzle-kit side

AndriiSherman avatar Apr 13 '23 19:04 AndriiSherman

Fixed in [email protected]

AndriiSherman avatar May 25 '23 20:05 AndriiSherman