© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•12mo ago•
1 reply
loliklr:)

Using date object for sqlite in INSERT INTO SELECT

I'm struggling with an
insert into select
insert into select
query in SQLite where I need to provide custom timestamp values.

Here's my table definition:
export const games = sqliteTable("games", {
  id: text("id", { length: 36 })
    .primaryKey()
    .$defaultFn(() => randomUUID()),
  playerId: text("doctor_id")
    .notNull()
    .references(() => users.id),
  name: text("name").notNull(),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull(),
  updatedAt: integer("updated_at", { mode: "timestamp" }).notNull(),
  deletedAt: integer("deleted_at", { mode: "timestamp" }),
});
export const games = sqliteTable("games", {
  id: text("id", { length: 36 })
    .primaryKey()
    .$defaultFn(() => randomUUID()),
  playerId: text("doctor_id")
    .notNull()
    .references(() => users.id),
  name: text("name").notNull(),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull(),
  updatedAt: integer("updated_at", { mode: "timestamp" }).notNull(),
  deletedAt: integer("deleted_at", { mode: "timestamp" }),
});


I'm trying to insert a new game record by selecting some fields from the users table, but I want to provide custom timestamps for
createdAt
createdAt
and
updatedAt
updatedAt
(they should be when the game started, not when the DB insert happens). Here's what I tried:

const result = await db.insert(games).select(
  db
    .select({
      playerId: users.id,
      name: users.firstName, // Need current name from users table
      createdAt: sql<Date>`${gameStartDate}`.as("created_at"),
      updatedAt: sql<Date>`${gameStartDate}`.as("updated_at"),
    })
    .from(users)
    .where(eq(users.id, ctx.user.id))
);
const result = await db.insert(games).select(
  db
    .select({
      playerId: users.id,
      name: users.firstName, // Need current name from users table
      createdAt: sql<Date>`${gameStartDate}`.as("created_at"),
      updatedAt: sql<Date>`${gameStartDate}`.as("updated_at"),
    })
    .from(users)
    .where(eq(users.id, ctx.user.id))
);


This throws an error because SQLite seems confused about the date format. What's the correct way to provide custom timestamp values in an
insert into select
insert into select
query with Drizzle?

Thanks for any help!
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Infer insert model for select in "Insert into ... select"
Drizzle TeamDTDrizzle Team / help
15mo ago
Nested select in an insert
Drizzle TeamDTDrizzle Team / help
15mo ago
insert from select
Drizzle TeamDTDrizzle Team / help
12mo ago
Sqlite insert returning typing help
Drizzle TeamDTDrizzle Team / help
3y ago