Skip to content

Set-operation orderBy() ignores casing config (pg + sqlite), producing a non-existent column reference #5987

Description

@tsushanth

Description

With casing: 'snake_case' (columns declared without an explicit DB name), ordering a set operation (union/intersect/except/…) by a column emits the JS property name in ORDER BY instead of the cased DB column name. The SELECT list is cased correctly, so ORDER BY references a column that isn't in the result set — the query fails at runtime (Postgres: column "firstName" does not exist). Affects pg (bare and asc()/desc()-wrapped) and sqlite (bare-column path). mysql/singlestore are unaffected.

Steps to reproduce

import { pgTable, text, PgDialect, QueryBuilder } from 'drizzle-orm/pg-core';
import { asc, union } from 'drizzle-orm';

const users = pgTable('users', { firstName: text() }); // no explicit db name
const dialect = new PgDialect({ casing: 'snake_case' });
const qb = new QueryBuilder(dialect);

union(qb.select().from(users), qb.select().from(users)).orderBy(asc(users.firstName));

Generated SQL:

observed: (select "first_name" from "users") union (select "first_name" from "users") order by "firstName" asc
expected: (select "first_name" from "users") union (select "first_name" from "users") order by "first_name" asc

A plain select().orderBy(asc(users.firstName)) is correct (order by "users"."first_name" asc); only set-operation ordering is affected.

Root cause

buildSetOperationQuery emits the raw column .name instead of casing.getColumnCasing(...):

  • drizzle-orm/src/pg-core/dialect.ts:483 (bare column) and :489 (sql-wrapped chunk)
  • drizzle-orm/src/sqlite-core/dialect.ts:478 (bare column)

The correct pattern is already used in mysql-core/dialect.ts:528,536, singlestore-core/dialect.ts, and sqlite-core/dialect.ts:485.

Suggested fix

Replace those three sites with this.casing.getColumnCasing(<column>) (the column object, matching mysql). After the change, all set-operation ORDER BY clauses emit "first_name", and plain selects are unchanged.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/fixed-in-betaThis bug has been fixed in beta (or will be soon).

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions