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]: References across schemas does not include schema name in generated migrations #440
Comments
This also happens when defining only a single pgSchema() attribute. Table creation works, but all ALTER TABLE statements are missing the schema reference.
|
Fixed in |
Hello, drizzle-kit: v0.19.12 The bug seems to be back :( With this schema: const authSchema = pgSchema("auth")
const authUsers = authSchema.table("users", {
id: uuid("id").primaryKey().notNull(),
})
export const users = pgTable("users", {
id: uuid("id").primaryKey().notNull().references(() =>authUsers.id),
}) Generated SQL: CREATE TABLE IF NOT EXISTS "users" (
"id" uuid PRIMARY KEY NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "users" ADD CONSTRAINT "users_id_users_id_fk" FOREIGN KEY ("id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$; I can open a new issue if you want ;) |
@rphlmr, I now understand why you see an issue here. We have one final major issue with drizzle-kit in PostgreSQL only. If different schemas have tables with the same name, drizzle-kit will not function as expected. We are aware that this is the primary purpose of schemas in PostgreSQL. We will work on fixing it as soon as possible For example, this one will work as expected const authSchema = pgSchema("auth")
const authUsers = authSchema.table("auth_users", {
id: uuid("id").primaryKey().notNull(),
})
export const users = pgTable("users", {
id: uuid("id").primaryKey().notNull().references(() =>authUsers.id),
}) CREATE TABLE IF NOT EXISTS "users" (
"id" uuid PRIMARY KEY NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "users" ADD CONSTRAINT "users_id_auth_users_id_fk" FOREIGN KEY ("id") REFERENCES "auth_users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$; |
drizzle-kit: v0.19.12
in my case, generates
which should be
since app_site is in test schema, not core. seems like need a seems like the author's case would still be broken also, or at least any case where from table and to table are in schemas other than public. and it's not because the tables are the same name, because mine are not. |
What version of
drizzle-orm
are you using?0.23.10
Describe the Bug
When creating a reference to a table in a different schema the foreign key constraint generated in the migration does not include the custom schema name resulting in an error.
results in the below foreign key constraint
adding the schema name
"auth"."users"("id")
allows the migration to run successfully.The text was updated successfully, but these errors were encountered: