drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Support nested schema/table input for defineRelations in Relations v2

Open
#5,483 0 comments 0 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

I would like `defineRelations` to support a nested schema/table object shape for multi-schema projects, while preserving the same runtime behavior and type inference as the current flat `schema.table` shape used by the Relations v2 API. The current Relations v2 work is still evolving, and there are already related discussions around multi-schema usage and relation ergonomics.

Today, this works:
```ts
import { defineRelations } from "drizzle-orm";

export const relations = defineRelations(flatSchemas, ({ one, many, ...s }) => ({
"identity.person": {
birth_country: one["location.country"]({
from: s["identity.person"].birth_country_code,
to: s["location.country"].id,
}),
},
}));
```

but my suggestion would be like this:
```ts
const schemas = {
identity: { person },
location: { country },
}

defineRelationsMultiSchemas(schemas, ({ one, many, ...s }) => ({
identity: {
person: {
birth_country: one.location.country({
from: s.identity.person.birth_country_code,
to: s.location.country.id,
}),
},
},
}))
```

Contributor guide

Open the contributing guide

Research direction

Start with the existing Relations v2 `defineRelations` API and its flat `schema.table` example, then compare it with the proposed nested schema/table shape for multi-schema projects. Done means the nested input is supported while preserving the current runtime behavior and type inference.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.