Date being overridden in neon and drizzle orm

Hey! when I'm trying to store a date in drizzle orm and neon the date that's being written is overwritten here is the table:
export const meetings = pgTable(
"meeting",
{
id: serial("id").primaryKey(),
name: varchar("name", { length: 256 }),
isEvent: boolean("isEvent").notNull().default(false),
location: varchar("location", { length: 256 }),
isPublic: boolean("isPublic").notNull().default(false),
isRequired: boolean("isRequired").notNull().default(false),
link: text("link"),
date: date("date", {
mode: "date",
}).notNull(),
createdById: varchar("createdById", { length: 255 })
.notNull(),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt"),
},
(example) => ({
createdByIdIdx: index("userCreatedBy_idx").on(example.createdById),
nameIndex: index("name_idx").on(example.name),
dateIndex: index("date_idx").on(example.date),
})
);


export const attendedMeetings = pgTable(
"attended_meetings",
{
userId: varchar("userId", { length: 255 })
.notNull()
.references(() => users.id),
meetingId: integer("meetingId")
.notNull()
.references(() => meetings.id),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
},
(example) => ({
pk: primaryKey({ columns: [example.userId, example.meetingId] }),
userIdIdx: index("userId_idx").on(example.userId),
meetingIdIdx: index("meetingId_idx").on(example.meetingId),
})
);
export const meetings = pgTable(
"meeting",
{
id: serial("id").primaryKey(),
name: varchar("name", { length: 256 }),
isEvent: boolean("isEvent").notNull().default(false),
location: varchar("location", { length: 256 }),
isPublic: boolean("isPublic").notNull().default(false),
isRequired: boolean("isRequired").notNull().default(false),
link: text("link"),
date: date("date", {
mode: "date",
}).notNull(),
createdById: varchar("createdById", { length: 255 })
.notNull(),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt"),
},
(example) => ({
createdByIdIdx: index("userCreatedBy_idx").on(example.createdById),
nameIndex: index("name_idx").on(example.name),
dateIndex: index("date_idx").on(example.date),
})
);


export const attendedMeetings = pgTable(
"attended_meetings",
{
userId: varchar("userId", { length: 255 })
.notNull()
.references(() => users.id),
meetingId: integer("meetingId")
.notNull()
.references(() => meetings.id),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
},
(example) => ({
pk: primaryKey({ columns: [example.userId, example.meetingId] }),
userIdIdx: index("userId_idx").on(example.userId),
meetingIdIdx: index("meetingId_idx").on(example.meetingId),
})
);
on the create tRPC endpoint:
create: adminProcedure
.input(createInput)
.mutation(async ({ ctx, input }) => {
// 2024-04 - 25T21: 30:00.000Z -> 2024-04 - 25T06:00:00.000Z (left = correct (what's being passed in), right = wrong but what's stored in db)
await ctx.db.insert(meetings).values(input)
}),
create: adminProcedure
.input(createInput)
.mutation(async ({ ctx, input }) => {
// 2024-04 - 25T21: 30:00.000Z -> 2024-04 - 25T06:00:00.000Z (left = correct (what's being passed in), right = wrong but what's stored in db)
await ctx.db.insert(meetings).values(input)
}),
Is there some default postgres behavior I am unaware of?
Solution:
The date type doesn't contain hour/minute/second data and thus doesn't map well to a js date, it may be a good idea to try to use string mode instead or not use the date type at all and use a timestamp instead
Jump to solution
2 Replies
Solution
Tiedye
Tiedye8mo ago
The date type doesn't contain hour/minute/second data and thus doesn't map well to a js date, it may be a good idea to try to use string mode instead or not use the date type at all and use a timestamp instead
Ani
AniOP8mo ago
switching to timestamp worked like a charm!
Want results from more Discord servers?
Add your server