drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]:Column mapping inconsistency between snake_case database columns and camelCase schema definitions

Open Abhishek21k opened this issue 8 months ago • 2 comments

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.41.0

What version of drizzle-kit are you using?

0.30.6

Other packages

No response

Describe the Bug

I've encountered an issue where Drizzle ORM is not correctly mapping between camelCase property names in schema definitions and snake_case column names in the database. Undesired behavior: When using Drizzle ORM to query a table with snake_case column names (e.g., is_admin), the result object is missing properties that should be mapped to camelCase (e.g., isAdmin). However, when executing a raw SQL query on the same table, all fields including the missing ones are returned correctly. Steps to reproduce:

Create a schema with camelCase property names that map to snake_case column names:

typescriptexport const membersTable = pgTable("members", {
  id: serial("id").primaryKey(),
  email: varchar("email", { length: 255 }).notNull().unique(),
  companyName: varchar("company_name", { length: 255 }).unique().notNull(),
  createdAt: timestamp("created_at").defaultNow(),
  updatedAt: timestamp("updated_at").defaultNow(),
  isAdmin: boolean("is_admin").default(false).notNull(),
});

Query the table using Drizzle ORM:

typescriptconst result = await db
  .select()
  .from(membersTable)
  .where(eq(membersTable.email, email))
  .limit(1);

console.log(result[0]);

Observe that the result is missing the isAdmin property:

javascript{
  id: 861,
  email: '[email protected]',
  companyName: 'Company',
  createdAt: 2025-04-09T09:13:09.966Z,
  updatedAt: 2025-04-09T09:13:09.966Z
  // isAdmin is missing!
}

Execute a raw SQL query on the same table:

typescriptconst rawResult = await db.execute(sql`
  SELECT * FROM members WHERE email = ${email} LIMIT 1
`);
console.log(rawResult.rows[0]);

Observe that the raw result correctly includes all columns:

javascript{
  id: 861,
  email: '[email protected]',
  company_name: 'Company',
  created_at: '2025-04-09 09:13:09.966078',
  updated_at: '2025-04-09 09:13:09.966078',
  is_admin: true  // This field is present in raw SQL but missing in Drizzle ORM query
}

Desired result: When using Drizzle ORM to query a table, all fields defined in the schema should be returned in the result object, with appropriate mapping from snake_case column names to camelCase property names.

Abhishek21k avatar Apr 09 '25 18:04 Abhishek21k

#3094

drahmedshaheen avatar May 01 '25 08:05 drahmedshaheen

Hey everyone!

I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.

I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!

We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.


First branch is drizzle-kit rewrite

All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439

As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.

What's left here:

  • We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the srid issue here as well).
  • We are finishing commutative migrations for all dialects.
  • We are finishing up the command, so the migration flow will be as simple as drizzle-kit up for you.

Where it brings us:

  • We are getting drizzle-kit into a new good shape where we can call it [email protected]!

Timeline:

  • We need ~2 weeks to finish all of the above and send this branch to beta for testing.

Second big branch is a complex one with several HUGE updates

  • Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)

TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k

You can read more about it here.

What's left here:

Where it brings us:

  • We are getting drizzle-orm into a new good shape where we can call it [email protected]!

Breaking changes:

  • We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.

Third branch is adding support for CockroachDB and MSSQL dialects

Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.

Summary

All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.

We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).

We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.

Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!

Timelines

We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!

AndriiSherman avatar Aug 30 '25 19:08 AndriiSherman

Hi @Abhishek21k. I tried to reproduce your issue, but wasn't able to. Here is the example I tested, and it worked as expected:

import { drizzle } from 'drizzle-orm/node-postgres';
import pg from 'pg';
import { boolean, pgTable, serial, timestamp, varchar } from 'drizzle-orm/pg-core';
import { eq, sql } from 'drizzle-orm';

const { Pool } = pg;

const membersTable = pgTable("members", {
  id: serial("id").primaryKey(),
  email: varchar("email", { length: 255 }).notNull().unique(),
  companyName: varchar("company_name", { length: 255 }).unique().notNull(),
  createdAt: timestamp("created_at").defaultNow(),
  updatedAt: timestamp("updated_at").defaultNow(),
  isAdmin: boolean("is_admin").default(false).notNull(),
});

const main = async () => {
  const pool = new Pool({
    host: 'localhost', 
    port: 5432, 
    user: 'postgres', 
    password: 'postgres', 
    database: 'postgres', 
    ssl: false
  });
  const db = drizzle({ client: pool });

  const email = '[email protected]';
  const result = await db
  .select()
  .from(membersTable)
  .where(eq(membersTable.email, email))
  .limit(1);

  console.log(result[0]);
  
  const rawResult = await db.execute(sql`SELECT * FROM members WHERE email = ${email} LIMIT 1`);
  console.log(rawResult.rows[0]);

  await pool.end()
  
}

main()

output:

{
  id: 1,
  email: '[email protected]',
  companyName: 'aa',
  createdAt: 2025-12-04T13:49:32.608Z,
  updatedAt: 2025-12-04T13:49:32.608Z,
  isAdmin: false
}
{
  id: 1,
  email: '[email protected]',
  company_name: 'aa',
  created_at: '2025-12-04 13:49:32.608427',
  updated_at: '2025-12-04 13:49:32.608427',
  is_admin: false
}

I used [email protected] and [email protected]. I’ll close this issue for now. Please tag me (@OleksiiKH0240) when you provide reproduction steps or a minimal repo that reproduces the issue, and I will reopen it.

OleksiiKH0240 avatar Dec 04 '25 13:12 OleksiiKH0240