drizzle-team / drizzle-team/drizzle-orm

[BUG]: "Insert into ... select" expects all columns

Open
#3,608 8 comments 46 reactions 2 assignees Claimed by @Sukairo-02 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.36.4

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

N/A

### Other packages

_No response_

### Describe the Bug

Using the new ["Insert into... select" feature](https://orm.drizzle.team/docs/insert#insert-into--select), the implementation seems to expect all table columns to be present in the select. [Here](https://github.com/drizzle-team/drizzle-orm/blob/30e5347c5b363e4ae5e30be78ac0bf64feaf98d4/drizzle-orm/src/pg-core/query-builders/insert.ts#L90) is the relevant part in the source code. In SQL, this is not required and honestly in many cases undesirable. I am not sure if the current implementation would work for any real-life example...

Here is a reproduction repo:

If you run this, it will fail with the following:

```
Error: Insert select error: selected fields are not the same or are in a different order compared to the table definition
at PgInsertBuilder.select (/home/projects/stackblitz-starters-ll4899/node_modules/drizzle-orm/pg-core/query-builders/insert.cjs:86:13)
```

I believe the corresponding SQL statement is valid and executable. You don't really want to explicitly supply `id` in this use case.

```sql
insert into transaction (type, user_id)
select 'Stuff' as type, user_id
from user
where user.name = 'Bob';
```

I think this is an issue across all supported dialects.

PS. The documentation examples seem to be inserting 1-2 columns at most so that's kinda misleading given the above.

_I raised this on [Discord](https://discord.com/channels/1043890932593987624/1043890932593987627/1308643047558479933) but I think it got lost amongst all the messages._

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.