drizzle-team / drizzle-team/drizzle-orm

[BUG]: same name for two distinct columns in schema but `generate` does not fail and generate a flawed .sql file

Open
#3,325 1 comment 0 reactions 1 assignee Claimed by @L-Mario564 View on GitHub
bug bug/fixed-in-beta drizzle/kit has-pr improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.35.3

### What version of `drizzle-kit` are you using?

0.26.2

### Describe the Bug

I have the following in my `schema.sql`:
```ts
export const postContentTable = pgTable("post_content", {
id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
postId: integer("id").references(() => postTable.id).notNull(),
postProofId: integer("post_proof_id")
.notNull()
.unique()
.references(() => postProofTable.id), // cannot point to deletion proof
parentId: integer("parent_id").references((): AnyPgColumn => postContentTable.id), // not null if edit
title: varchar("title", { length: MAX_LENGTH_TITLE }).notNull(),
body: varchar("body"),
pollId: integer("poll_id").references((): AnyPgColumn => pollTable.id), // for now there is only one poll per post at most
createdAt: timestamp("created_at", {
mode: "date",
precision: 0,
})
.defaultNow()
.notNull()
});
```

As you can see I made a mistake and called both`postId` and `id` columns the same name: `id`.

Instead of complaining during `generate`, it does generate the following .sql:

```sql
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "post_content" (
"id" integer NOT NULL,
"post_proof_id" integer NOT NULL,
"parent_id" integer,
"title" varchar(130) NOT NULL,
"body" varchar,
"poll_id" integer,
"created_at" timestamp (0) DEFAULT now() NOT NULL,
CONSTRAINT "post_content_post_proof_id_unique" UNIQUE("post_proof_id")
);
```

Missing the `postId` field, and the `id` is not primaryKey anymore.

Which later breaks my `migrate` when I try to call the `id` field as a foreign key:
```
ERROR: Migration V0000__secret_senator_kelly.sql failed
------------------------------------------------
SQL State : 42830
Error Code : 0
Message : ERROR: there is no unique constraint matching given keys for referenced table "post_content"
Where: SQL statement "ALTER TABLE "comment_content" ADD CONSTRAINT "comment_content_post_content_id_post_content_id_fk" FOREIGN KEY ("post_content_id") REFERENCES "public"."post_content"("id") ON DELETE no action ON UPDATE no action"
PL/pgSQL function inline_code_block line 2 at SQL statement
Location : /flyway/sql/V0000__secret_senator_kelly.sql (/flyway/sql/V0000__secret_senator_kelly.sql)
Line : 275
Statement : --> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "comment_content" ADD CONSTRAINT "comment_content_post_content_id_post_content_id_fk" FOREIGN KEY ("post_content_id") REFERENCES "public"."post_content"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$
```

Not very explicit error, since at this stage, `migrate` is just supposed to take my `.sql` for granted!

### Expected behavior

I would have expected the `generate` target to fail with an explicit error telling me I used the same key for two distinct columns.
I was lucky to catch it at migrate. I could have caught it only in production...

### Environment & setup

Debian 12.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.