Drizzle complains when I'm trying to insert into a table.

I have a schema that looks like this:
export const Organization = pGtable(
"Organization",
{
id: text("id").primaryKey().notNull(),
name: text("name").notNull(),
createdAt: timestamp("createdAt", { precision: 3, mode: "string" })
.defaultNow()
.notNull(),
updatedAt: timestamp("updatedAt", {
precision: 3,
mode: "string",
}).notNull(),
},
(table) => {
return {
name_key: uniqueIndex("Organization_name_key").using(
"btree",
table.name,
),
};
},
);
export const Organization = pGtable(
"Organization",
{
id: text("id").primaryKey().notNull(),
name: text("name").notNull(),
createdAt: timestamp("createdAt", { precision: 3, mode: "string" })
.defaultNow()
.notNull(),
updatedAt: timestamp("updatedAt", {
precision: 3,
mode: "string",
}).notNull(),
},
(table) => {
return {
name_key: uniqueIndex("Organization_name_key").using(
"btree",
table.name,
),
};
},
);
and when I'm trying to run a query against it:
import * as schema from "../src/drizzle/schema";
import { Client } from "pg";

export const client = new Client({
connectionString: process.env.DATABASE_URL,
});

export const db = drizzle(client, { schema });

await db.insert(Organization).values({
id: "68184833-8d03-41d6-95a2-4f23c4f097d6",
name: "org",
createdAt: new Date(),
updatedAt: new Date(),
});
import * as schema from "../src/drizzle/schema";
import { Client } from "pg";

export const client = new Client({
connectionString: process.env.DATABASE_URL,
});

export const db = drizzle(client, { schema });

await db.insert(Organization).values({
id: "68184833-8d03-41d6-95a2-4f23c4f097d6",
name: "org",
createdAt: new Date(),
updatedAt: new Date(),
});
typescript complains that my data is wrong:
Object literal may only specify known properties, and 'id' does not exist in type '{ id: string | SQL<unknown> | Placeholder<string, any>; ...
Object literal may only specify known properties, and 'id' does not exist in type '{ id: string | SQL<unknown> | Placeholder<string, any>; ...
if I add as any at the end it works, but I'd rather not opt-out of type-safety. What am I doing wrong?
2 Replies
Adam Akiva
Adam Akiva2d ago
In order to fix your issue what you need to do is to transform the Date objects to String using (for example):
createdAt: new Date().toISOString()
createdAt: new Date().toISOString()
I tested your code snippet and the error is indeed on the id for some reason, but this solution should resolve your issue (worked for me at least). It might be worth to do additional digging to find out why the error is displayed on the id field.
bloberenober
bloberenober2d ago
you can also change mode to date for createdAt and updatedAt columns instead