drizzle-team / drizzle-team/drizzle-orm

[BUG]: `INSERT INTO table SELECT` requires alias

Open
#3,656 3 comments 3 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.36.4

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

0.28.1

### Other packages

_No response_

### Describe the Bug

```js
// schema.js

import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';

export const userTable = sqliteTable('user', {
id: integer().primaryKey(),
name: text().notNull(),
});

export const employeeTable = sqliteTable('employee', {
id: integer().primaryKey(),
name: text().notNull(),
joinedAt: integer({ mode: 'timestamp' }).notNull(),
});

```

This works without runtime errors:

```js
import { sql } from 'drizzle-orm';
import { db } from './src/db/client.js';
import { employeeTable, userTable } from './src/db/schema.js';

await db.insert(userTable).values([{ name: 'Hyunbin' }]);

await db.insert(employeeTable).select(
db
.select({
id: sql`null`, // Reference https://github.com/drizzle-team/drizzle-orm/issues/3608
name: userTable.name,
joinedAt: sql`unixepoch()`,
})
.from(userTable),
);
```

```sql
insert into "employee" ("id", "name", "joinedAt") select null, "name", unixepoch() from "user"
```

Yet, it expects column aliases:

> Type 'DrizzleTypeError<"You cannot reference this field without assigning it an alias first - use `.as()`">' is not assignable to type 'Date | SQL | Aliased | AnySQLiteColumn'.ts(2769)

Contributor guide

Open the contributing guide

Research direction

Reproduce the TypeScript example from schema.js using the insert().select() entry point and the db client.js setup. Inspect the type checking around the null, name, and joinedAt select fields, then verify that the query still generates the shown INSERT ... SELECT SQL without requiring aliases and that the example type-checks successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
sqlite, typescript
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.