drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.52k stars 643 forks source link

[BUG]: SQLITE Schema - Integer mode 'timestamp' in not compatible with (unixepoch()) #2912

Closed palak-convivity closed 3 weeks ago

palak-convivity commented 2 months ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.24.2

Describe the Bug

If I set mode:'timestamp' in drizzle schema updated_at key then the update query is not working below is the example code:

export const messages = sqliteTable('messages', {
  id: integer('id').primaryKey(),
  type: text('type'),
  message: text('message'),
  userId: integer('user_id'),
  chatId: integer('chat_id'),
  history: text('history', { mode: 'json' }),
  files: text('files', { mode: 'json' }),
  reaction: text('reaction', { mode: 'json' }).$type<any[]>(),
  solution: integer('solution', { mode: 'boolean' }),
  deleted: integer('deleted', { mode: 'boolean' }),
  replyTo: integer('replyTo'),
  closed: integer('closed', { mode: 'boolean' }).notNull().default(false),
  createdAt: integer('created_at', { mode: 'timestamp' })
    .notNull()
    .default(sql`(unixepoch())`),
  updatedAt: integer('updated_at', { mode: 'timestamp' })
    .default(sql`(unixepoch())`)
    .$onUpdate(() => sql`(unixepoch())`),
});

this is the schema I am using

    const update = await db(this.env.HOMEWORK_HELP_D1)
      .update(messages)
      .set({ message: payload.message, chatId: payload.chatId })
      .where(eq(messages.id, payload.messageId))
      .returning();

this is the query I was trying to execute...

Expected behavior

Now, the expected behavior is that message should get update but the query is not working... | If mode:'timestamp' is removed from scheme in updated_at then it works... there might be a conflict between the type of mode: timestamp and that is returned by (unixepoch())

Environment & setup

node - v20.13.1 npm - 10.8.2

package.json


{
  "name": "project",
  "version": "0.0.0",
  "scripts": {
    "deploy": "wrangler deploy",
    "dev": "wrangler dev",
    "start": "wrangler dev",
    "cf-typegen": "wrangler types",
    "generate": "npx drizzle-kit generate",
    "push": "wrangler d1 execute homeworkhelp --local --file=migrations/*.sql"
  },
  "devDependencies": {
    "@cloudflare/workers-types": "^4.20240725.0",
    "@types/cookie": "^0.6.0",
    "drizzle-kit": "^0.24.2",
    "typescript": "^5.5.2",
    "wrangler": "^3.60.3"
  },
  "dependencies": {
    "@aws-sdk/client-s3": "^3.620.0",
    "@aws-sdk/s3-request-presigner": "^3.620.0",
    "@tsndr/cloudflare-worker-jwt": "^2.5.3",
    "cookie": "^0.6.0",
    "drizzle-orm": "^0.30.10",
    "nanoid": "^5.0.7",
    "partyserver": "^0.0.45"
  }
}
L-Mario564 commented 3 weeks ago

This is an issue with SQLite itself, not Drizzle. To solve it, you can try doing strftime('%s', 'now') instead of unixepoch() in insert/update queries.