[BUG]: no columns are converted to snake_case before executing insert statements
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
What is the undesired behavior?
Automatic column name conversion to "snake_case" doesn't happen, resulting in an error when using insert() method to create rows in postgres database with neon-postgres connector
What are the steps to reproduce it?
- Initialize
drizzle.config.tsfile:
export default defineConfig({
out: "./src/db/migrations",
schema: "./src/db/schema",
dialect: "postgresql",
dbCredentials: {
url: DB_URL!,
},
casing: "snake_case",
strict: true,
verbose: true,
});
- Create a schema for user:
export const userTable = pgTable("user", {
id: uuid().primaryKey().defaultRandom(),
firstName: varchar({ length: 50 }).unique().notNull(),
lastName: varchar({ length: 50 }),
email: varchar({ length: 200 }).notNull().unique(),
mobile: char({ length: 10 }).notNull().unique(),
hash: varchar().notNull(),
salt: varchar().notNull(),
...timestamps(),
});
- generate migrations and push migrations with
drizzle-kitresulting in the following file:
CREATE TABLE IF NOT EXISTS "user" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"first_name" varchar(50) NOT NULL,
"last_name" varchar(50),
"email" varchar(200) NOT NULL,
"mobile" char(10) NOT NULL,
"hash" varchar NOT NULL,
"salt" varchar NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
"updated_at" timestamp with time zone,
CONSTRAINT "user_firstName_unique" UNIQUE("first_name"),
CONSTRAINT "user_email_unique" UNIQUE("email"),
CONSTRAINT "user_mobile_unique" UNIQUE("mobile")
);
- write a custom utility function and test insertion:
async function addUser() {
const pwd = await generateHashPassword("sometpaswd");
await db
.insert(userTable)
.values({
email: "[email protected]",
firstName: "SSS",
mobile: "9371828430",
hash: pwd.hash,
salt: pwd.salt,
})
}
addUser();
What is the desired result? The data gets inserted without any error
Error displayed
> npx tsx src/db/queries/test.ts
X:\Web Projects\stem-for-society-api\node_modules\pg-pool\index.js:45
Error.captureStackTrace(err)
^
error: column "firstName" of relation "user" does not exist
at X:\Web Projects\stem-for-society-api\node_modules\pg-pool\index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at addUser (X:\Web Projects\stem-for-society-api\src\db\queries\test.ts:7:3) {
length: 126,
severity: 'ERROR',
code: '42703',
detail: undefined,
hint: undefined,
position: '27',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_target.c',
line: '1071',
routine: 'checkInsertTargets'
}
Node.js v20.14.0
When using .getSQL() the result is this:
{
sql: 'insert into "user" ("id", "firstName", "lastName", "email", "mobile", "hash", "salt", "createdAt", "updatedAt") values (default, $1, default, $2, $3, $4, $5, default, $6)',
params: [
'SSS',
'[email protected]',
'9371828430',
'd840a18364a231cf35581a7279ee7e6558504e0108578352d37682e9ff7f9ea33ee2303a1d237c1f6a53500900fa62c6e1e82f729ad26333ed0eaefbda689f4d6c7c0ddcb02e7396fed97282358aed111957a722ee49b7405235d974de46e937f9255322255268a3cab1f1f0d15c972b3ff7f84be41a707dfe5bc262fcde364d0e75eaffcc96c309090db3bb13b8ec47b077c49951eb634341fdc502b167734ca4ded644daf689b592907dea4aa566eb9eddf199fadcb5dc63390e2f3818b5b319d33dae80118d7592fa6c9209a4fdcb6a33d8bdbe7c688921fa03208473759392ed73402b554f76b8d1fce3e8f67fc3bedaa942ff4981805066f999ac0db54e', 'a0e05260735f92e7b64726b10cb4a5b78e59bdbaf0b1c2099c7ce5c19db562a0765deec8ca218a0a43cdda04c83d2e8a4917d22219e708ab4785f14e7327b907',
'2024-11-30T10:11:41.621Z'
]
}
it is clearly seen that the column names are indeed not converted to "snake_case" and still reflects the same key name from table declaration, hence resulting in this error
If you need any other information, please feel free to ask and I can provide.
Also happens with .select() queries. As it seems related, I will include the relevant line of code here as well.
const allPosts = await db.select().from(posts);
schema:
export const posts = pgTable('post', {
id: serial().primaryKey(),
createdAt: timestamp().defaultNow().notNull(),
I had this problem and it turned out that I had to declare casing: 'snake_case' in 2 places. One was drizzle.config.ts as you have done. The other is in the actual drizzle connection.
I have drizzle.config.ts as
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
schema: './src/**/schema.ts',
out: './drizzle',
dialect: 'postgresql',
casing: "snake_case",
dbCredentials: {
url: process.env.DATABASE_URL,
},
});
and my db connection set up like
import { drizzle } from 'drizzle-orm/postgres-js';
import * as schema from '@/db/schema';
import env from "@/env";
export const db = drizzle({
connection: {
url: env.DATABASE_URL,
casing: 'snake_case',
schema,
logger: true,
});
export type db = typeof db;
Once I did this all the problems you describe above went away for me.
I can recommend watching ttps://www.youtube.com/watch?v=TIov_V6NeuQ by CJ on Syntax which cleared up the issue for me.
it was extremely illogical for me and I spent some time until I understood what was going on
not good DX 😓
I had this problem and it turned out that I had to declare casing: 'snake_case' in 2 places. One was drizzle.config.ts as you have done. The other is in the actual drizzle connection.
This solution suggested above worked for me. Thanks!
Here are the docs for casing: https://orm.drizzle.team/docs/sql-schema-declaration#camel-and-snake-casing.
Here are the docs for casing: https://orm.drizzle.team/docs/sql-schema-declaration#camel-and-snake-casing.
I think it would be useful if this section had reference also to defineConfig casing option doe drizzle.config.ts file
and docs for defineConfig seems to be not covering new casing option
Just wanna add my 2 cents here. If you want to pull your db schema (introspect) with the same casing it actually is, you have to set introspect.casing to 'preserve'
export default defineConfig({
// other options
introspect: {
casing: 'preserve',
}
});
Just wanna add my 2 cents here. If you want to pull your db schema (introspect) with the same casing it actually is, you have to set
introspect.casingto'preserve'export default defineConfig({ // other options introspect: { casing: 'preserve', } });
This solves it for me! Thank you @MosheSchnitzler 🙏