drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: defineRelations() should allow relation names that match column names (breaking change from 0.x)

Open
#5,506 0 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [x] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

# Description
In Drizzle ORM 0.x, relations() allowed defining a relation with the same name as an existing column on the table. In v1, defineRelations() throws a runtime error:

```
relations -> cardDefinitions: { user: r.one.users(...) }: relation name collides with column "user" of table "cardDefinitions"
```

# Reproduction

```
// schema.ts
export const cardDefinitions = pgTable('card_definitions', {
id: varchar('id', { length: 36 }).primaryKey(),
user: varchar('user', { length: 36 }).notNull().references(() => users.id),
card: varchar('card', { length: 36 }).notNull().references(() => cards.id),
definition: varchar('definition', { length: 255 }).notNull(),
});

// relations.ts
export const relations = defineRelations(schema, (r) => ({
cardDefinitions: {
user: r.one.users({ // <-- throws: collides with column "user"
from: r.cardDefinitions.user,
to: r.users.id,
}),
card: r.one.cards({ // <-- same issue
from: r.cardDefinitions.card,
to: r.cards.id,
}),
},
}));
```

# Use case

We implement a Stripe-like expand pattern where a field can be either a raw ID or an
expanded object depending on the API request:

```
// Without expand
{ "user": "usr_abc123", "card": "card_xyz789" }
```

```
// With ?expand=user,card
{ "user": { "id": "usr_abc123", "email": "..." }, "card": { "id": "card_xyz789", "term":
"..." } }
```

Having the relation name match the column name (user for both the FK column and the
relation) is essential for this pattern — the field name stays the same regardless of
whether it's expanded. This worked in 0.x with relations() and is a common API design
pattern (Stripe, GitHub, etc.).

# Expected behavior

defineRelations() should allow relation names that match column names. When both exist, the query result should use the relation (expanded object) when with includes it, and the column value (raw ID) otherwise — same as 0.x behavior.

# Environment

- drizzle-orm: 1.0.0-beta.17
- drizzle-kit: 1.0.0-beta.17
- Database: PostgreSQL
- Driver: node-postgres

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.