drizzle-team / drizzle-team/drizzle-orm

[BUG]: Composite primary key does not set type of columns to not null

Open
#3,901 4 comments 4 reactions 0 assignees View on GitHub
improvement qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [X] I have verified that the bug I'm about to report hasn't been filed before.

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

0.38.3

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

0.30.1

### Other packages

_No response_

### Describe the Bug

When setting a column to be a primaryKey inline as follows: `id: serial().primaryKey()` the types generated for that column understand that a primary key is implicitly not null, so when fetching the data the type will be `id: number;` or whatever the corresponding type for that column is.

However when using a composite primary key, the types generated don't seem to follow the same logic and are unaware of the implicit not null and so the type will be something like: `id: number | null;`;

Here's a full example:

```ts
import { integer, pgTable, primaryKey, serial } from "drizzle-orm/pg-core";

export const usersTable = pgTable("users", {
id: serial().primaryKey(),
});

export const friendsTable = pgTable(
"friends",
{
user1Id: integer(),
user2Id: integer(),
},
(table) => [primaryKey({ columns: [table.user1Id, table.user2Id] })]
);

// Expect: type User = { id: number; }
// Receive: type User = { id: number; }
type User = typeof usersTable.$inferSelect;

// Expect: type Friend = { user1Id: number; user2Id: number; }
// Receive: type Friend = { user1Id: number | null; user2Id: number | null; }
type Friend = typeof friendsTable.$inferSelect;
```

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.