drizzle-team / drizzle-team/drizzle-orm

[BUG]: .$returningId() doesn't work with primaryKey({ columns: [table.id], name: "..."})

Open
#3,551 0 comments 0 reactions 0 assignees View on GitHub
bug db/mysql priority 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.36.2

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

0.28.1

### Other packages

_No response_

### Describe the Bug

The `.$returningId()` function doesn't work with setting primaryKey like this:

```typescript
export const Declaracion = mysqlTable("Declaracion", {
id: int().autoincrement().notNull(),
fecha: date({ mode: 'date' }).notNull(),
empresaId: int().notNull().references(() => Empresa.id, { onDelete: "cascade", onUpdate: "cascade" } ),
},
(table) => {
return {
declaracionId: primaryKey({ columns: [table.id], name: "Declaracion_id"}),
}
});
```

It only works if you set the primaryKey this way,

```typescript
export const Declaracion = mysqlTable("Declaracion", {
id: int().autoincrement().notNull().primaryKey(),
fecha: date({ mode: 'date' }).notNull(),
empresaId: int().notNull().references(() => Empresa.id, { onDelete: "cascade", onUpdate: "cascade" } ),
},
(table) => {
return {
declaracionId: primaryKey({ columns: [table.id], name: "Declaracion_id"}),
}
});
```

You can see the difference more clearly here
```diff
export const Declaracion = mysqlTable("Declaracion", {
- id: int().autoincrement().notNull(), // DOES NOT WORK
+ id: int().autoincrement().notNull().primaryKey(), // IT WORKS THIS WAY
fecha: date({ mode: 'date' }).notNull(),
empresaId: int().notNull().references(() => Empresa.id, { onDelete: "cascade", onUpdate: "cascade" } ),
},
(table) => {
return {
declaracionId: primaryKey({ columns: [table.id], name: "Declaracion_id"}),
}
});
```
My complaint is that I already set the primaryKey here:

```typescript
(table) => {
return {
declaracionId: primaryKey({ columns: [table.id], name: "Declaracion_id"}),
}
```

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.