drizzle-orm: [BUG]: Column name not being returned as snake_case

What version of drizzle-orm are you using?

0.34.1

What version of drizzle-kit are you using?

0.25.0

Describe the Bug

When using casing: 'snake_case' in the config and defining a column like so firstName: t.text().notNull()

And then when accessing the column name via users.firstName.name we get firstName instead of first_name

Same happens if we get the columns via getTableConfig(users).columns and getTableColumns(users)

Expected behavior

No response

Environment & setup

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 31
  • Comments: 17

Most upvoted comments

You need to pass casing to drizzle() too, not only in the drizzle-kit config.

export const db = drizzle(client, { schema, casing: 'snake_case' })

This is my current workaround. I’ve committed to snake_case for the project so not a huge deal, but would be nice to inherit casing from the config.

import { toSnakeCase } from 'drizzle-orm/casing';

export const excluded = (column: PgColumn) => {
	return sql.raw(`excluded.${toSnakeCase(column.name)}`);
};

I can confirm getTableColumns(table) with the snake_casing settings turned on returns the columns incorrectly as camelCase.

Table was defined like shortId: varchar({ length: 21 })

 shortId: <ref *3> PgVarchar {
    name: 'shortId',
    keyAsName: true,
    primary: false,
    notNull: true,
    default: undefined,
    defaultFn: [Function (anonymous)],
    onUpdateFn: undefined,
    hasDefault: true,
    isUnique: false,
    uniqueName: 'deals_shortId_unique',
    uniqueType: undefined,
    dataType: 'string',
    columnType: 'PgVarchar',
    enumValues: undefined,
    generated: undefined,
    generatedIdentity: undefined,
    config: {
      name: 'shortId',
      keyAsName: true,
      notNull: true,
      default: undefined,
      hasDefault: true,
      primaryKey: false,
      isUnique: false,
      uniqueName: 'deals_shortId_unique',
      uniqueType: undefined,
      dataType: 'string',
      columnType: 'PgVarchar',
      generated: undefined,
      length: 21,
      enumValues: undefined,
      defaultFn: [Function (anonymous)]
    },
    table: PgTable {
      id: [PgUUID],
      shortId: [Circular *3],
      name: [PgVarchar],
      description: [PgVarchar],
      createdBy: [PgUUID],
      createdAt: [PgTimestamp],
      updatedAt: [PgTimestamp],
      deletedAt: [PgTimestamp],
      enableRLS: [Function: enableRLS],
      [Symbol(drizzle:Name)]: 'deals',
      [Symbol(drizzle:OriginalName)]: 'deals',
      [Symbol(drizzle:Schema)]: undefined,
      [Symbol(drizzle:Columns)]: [Circular *2],
      [Symbol(drizzle:ExtraConfigColumns)]: [Object],
      [Symbol(drizzle:BaseName)]: 'deals',
      [Symbol(drizzle:IsAlias)]: false,
      [Symbol(drizzle:IsDrizzleTable)]: true,
      [Symbol(drizzle:ExtraConfigBuilder)]: undefined,
      [Symbol(drizzle:PgInlineForeignKeys)]: [Array],
      [Symbol(drizzle:EnableRLS)]: false
    },
    length: 21
  },

It should be

shortId: <ref *3> PgVarchar {
    name: 'short_id',
    keyAsName: false,
    primary: false,
    notNull: true,
    default: undefined,
    defaultFn: [Function (anonymous)],
    onUpdateFn: undefined,
    hasDefault: true,
    isUnique: false,
    uniqueName: 'deals_short_id_unique',
    uniqueType: undefined,
    dataType: 'string',
    columnType: 'PgVarchar',
    enumValues: undefined,
    generated: undefined,
    generatedIdentity: undefined,
    config: {
      name: 'short_id',
      keyAsName: false,
      notNull: true,
      default: undefined,
      hasDefault: true,
      primaryKey: false,
      isUnique: false,
      uniqueName: 'deals_short_id_unique',

You need to pass casing to drizzle() too, not only in the drizzle-kit config.

export const db = drizzle(client, { schema, casing: 'snake_case' })

I guess it’s to be expected, but this doesn’t affect the column name mappings for tables outside the context of a query. I’ve written a convenience method for onConflictDoUpdate and I’m still getting the camelCase column name:

export const excluded = (column: PgColumn) => {
	return sql.raw(`excluded.${column.name}`);
}; // excluded.firstName

You need to pass casing to drizzle() too, not only in the drizzle-kit config.

export const db = drizzle(client, { schema, casing: 'snake_case' })

The missing part for me was the opposite. I included casing: 'snake_case' parameter in drizzle() but not in drizzle-kit config. I was following this guide, but configuring drizzle-kit is not mentioned there. Shall we mention drizzle-kit there?

I’m seeing the same issue (using Vercel Postgres)

Adding casing: 'snake_case' to both drizzle client and drizzle config does fix creating tables in snake case but it doesn’t return it in snake case when used with utils (e.g. getTableColumns) and with views which may lead drizzle studio to break

I was able to get my snake_case in the db right by specifying the casing in both drizzle.config.ts and /src/db/index.ts, I believe this should be specified in the docs as I have spent hours making this work

I have recently set up a fresh Supabase database (PostgreSQL)

my current setup is as follow

// package.json
"dependencies": {
    "drizzle-orm": "^0.42.0", // drizzle-orm
    "next": "15.3.1",
    "postgres": "^3.4.5", // postgres
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  },
  "devDependencies": {
    "@eslint/eslintrc": "^3",
    "@tailwindcss/postcss": "^4",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19",
    "drizzle-kit": "^0.31.0", // drizzle-kit
    "eslint": "^9",
    "eslint-config-next": "15.3.1",
    "tailwindcss": "^4",
    "typescript": "^5"
  }

my drizzle.config.ts

// drizzle.config.ts
import { defineConfig } from "drizzle-kit"

export default defineConfig({
  out: "./drizzle",
  schema: "./src/db/schema.ts",
  dialect: "postgresql",
  casing: "snake_case", // snake_case specified here
  dbCredentials: {
    url: process.env.DATABASE_URL!,
  },
})

my /src/db/index.ts

// /src/db/index.ts
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"

const client = postgres(process.env.DATABASE_URL!, { prepare: false })

export const db = drizzle({ client, casing: "snake_case" }) // snake_case specified here

my /src/db/schema.ts

// /src/db/schema.ts
import { pgTable, uuid, text, integer, timestamp } from "drizzle-orm/pg-core"

export const accountsTable = pgTable("accounts", {
  id: uuid().primaryKey().defaultRandom(),
  fullName: text().notNull(),
  age: integer(),
  createdAt: timestamp().defaultNow().notNull(),
  updatedAt: timestamp().defaultNow().notNull(),
})
// notice the properties are in camelCase

executing npx drizzle-kit generate resulted in this migration file

# /drizzle/0000_grey_psynapse.sql
CREATE TABLE "accounts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"full_name" text NOT NULL,
	"age" integer,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
# the columns have been converted to snake_case successfully

I’m guessing this is also related here.

When running drizzle-kit export the table names are also camelCased instead of snake_cased. drizzle-kit push works correctly.

Same issue as @kaelruland - getting camelCase when snake_case is needed for excluded. As a workaround, I added the snake_case column name to the column definition.

getTableColumns() returns columns in camelCase instead of snake_case in migrations