Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
hyunbinseo opened this issue Jan 19, 2025 · 2 comments
Open
1 task done

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

hyunbinseo opened this issue Jan 19, 2025 · 2 comments
Assignees
Labels
bug Something isn't working has-pr This issue has one or more PRs that that could close the issue when merged priority Will be worked on next qb/crud

Comments

@hyunbinseo
Copy link

Report hasn't been filed before.

  • 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. Use the mre-0 branch.

# Node.js 23
node src/index.ts

# Node.js 22
node --experimental-strip-types src/index.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)

// 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`),
});
@dakdevs
Copy link

dakdevs commented Jan 20, 2025

This is killing me rn with MySQL :(

@L-Mario564 L-Mario564 self-assigned this Feb 6, 2025
@L-Mario564 L-Mario564 added priority Will be worked on next qb/crud has-pr This issue has one or more PRs that that could close the issue when merged labels Feb 6, 2025
@WilcoKruijer
Copy link

Seems like INSERT INTO ... SELECT always needs to match precisely all columns of the table, which does not make sense considering generated columns but also nullable columns and columns with defaults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has-pr This issue has one or more PRs that that could close the issue when merged priority Will be worked on next qb/crud
Projects
None yet
Development

No branches or pull requests

4 participants