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?
3 Replies
Adam Akiva
Adam Akiva4w 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
bloberenober4w ago
you can also change mode to date for createdAt and updatedAt columns instead
addamsson
addamsson4w ago
thanks i ended up not migrating my project to drizzle though as the were too many problems with it
Want results from more Discord servers?
Add your server
More Posts
Is it possible to use the Query API to select many-to-many relations?In prisma I can do this to select the entities from the "other side" of a many-to-many join: ```jsonHow can I configure `drizzle-kit introspect` to generate a schema with a schema?I'm using [this](https://orm.drizzle.team/learn/migrate/migrate-from-prisma) guide to migrate from PCan't Push Schema to AWS Aurora - Is My Config Correct?Hi guys! I'm moving from Vercel Postgres to AWS Aurora and I'm just trying to `npx drizzle-kit pusNo matter what I try, npm run migrate defaults to my .env file. Npm run generate works just fine.Hey, I am trying to set dynamic variable paths. When I run npm run migrate, it always defaults to myconstruction a dynamic query builder generically ?Ok i'm not really sure how to ask this but I'm trying to allow a user the ability in UI to build outconflicting peer dependency when installing drizzle-ormI am trying to install drizzle orm in a fresh install of Nextjs and have the following error presentDatabase with no passwordOn my local machine for development, i have a psql database with no password. When i do '' for the ponConflictDoNothing still incrementing primaryKeyI have the following ballots schema and then inserting to it via an Next.js app directory API route.Optionally chaining joins and typescript issues.Hey everyone, I'm currently a bit stuck trying to brainstorm how to solve the following problem andUse COALESCE in indexHow can I generate this sql: ```sql CREATE UNIQUE INDEX "IndexName" ON "Table" ("foo", COALESCE ("ba