drizzle-team / drizzle-team/drizzle-orm

[BUG]: `INSERT INTO … SELECT` does not work with generated columns

Open
#3,971 7 comments 9 reactions 1 assignee Claimed by @L-Mario564 View on GitHub
bug bug/fixed-in-beta has-pr 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.38.4

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

0.30.2

### Other packages

_No response_

### Describe the Bug

[Reproduction](https://github.com/hyunbinseo/drizzle-playground/tree/mre-0). Use the `mre-0` branch.

```shell
# Node.js 23
node src/index.ts

# Node.js 22
node --experimental-strip-types src/index.ts
```

```ts
import { db } from './db/index.ts';
import { userTable } from './db/schema.ts';

await db // works
.insert(userTable)
.values({ deactivatedAt: new Date() })
.returning();

await db // fails
.insert(userTable)
.select(db.select({ deactivatedAt: userTable.deactivatedAt }).from(userTable));
```

> Error: Insert select error: selected fields are not the same or are in a different order compared to the table definition (at SQLiteInsertBuilder.select)

- https://github.com/drizzle-team/drizzle-orm/pull/2783

```ts
// schema.ts
import { sql, type SQL } from 'drizzle-orm';
import { integer, sqliteTable } from 'drizzle-orm/sqlite-core';

export const userTable = sqliteTable('user', {
id: integer().primaryKey(),
deactivatedAt: integer({ mode: 'timestamp' }),
isDeactivated: integer({ mode: 'boolean' }) //
.generatedAlwaysAs((): SQL => sql`${userTable.deactivatedAt} IS NOT NULL`),
});
```

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.