Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D1 DateTime type does not work #23479

Closed
brunocascio opened this issue Mar 13, 2024 · 8 comments
Closed

D1 DateTime type does not work #23479

brunocascio opened this issue Mar 13, 2024 · 8 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: dates / DateTime topic: driverAdapters topic: @prisma/adapter-d1 topic: sqlite
Milestone

Comments

@brunocascio
Copy link

brunocascio commented Mar 13, 2024

Bug description

By using DateTime field I get the following error when I make a query:

PrismaClientKnownRequestError: 
Invalid `prisma.example.findMany()` invocation:


Inconsistent column data: Conversion failed: number must be an integer in column 'createdAt', got '1709246273880.0'
    at In.handleRequestError (/Users/auser/projects/myproject/myproject-remix/node_modules/@prisma/client/runtime/library.js:122:6854)
    at In.handleAndLogRequestError (/Users/auser/projects/myproject/myproject-remix/node_modules/@prisma/client/runtime/library.js:122:6188)
    at In.request (/Users/auser/projects/myproject/myproject-remix/node_modules/@prisma/client/runtime/library.js:122:5896)
    at async l (/Users/auser/projects/myproject/myproject-remix/node_modules/@prisma/client/runtime/library.js:127:10871)

In fails both locally and at the edge (cloudflare)

How to reproduce

  1. Define the model
model Example {
  id            String         @id @default(cuid())
  createdAt     DateTime       @default(now())
  updatedAt     DateTime       @updatedAt
}
  1. Apply migrations
  2. Load some data without overriding createdAt updatedAt fields
  3. Run the query
prisma.example
  .findMany()
  .then(console.log)
  1. Error will be thrown

Expected behavior

Timestamp should be properly parsed as a Date

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

datasource db {
  provider = "sqlite"
  url = env("DATABASE_URL")
}

model Example {
  id            String         @id @default(cuid())
  createdAt     DateTime       @default(now())
  updatedAt     DateTime       @updatedAt
}
import { PrismaClient } from '@prisma/client'
import { PrismaD1 } from '@prisma/adapter-d1'
import { DefaultArgs } from '@prisma/client/runtime/library'

let prisma: PrismaClient<{ adapter: PrismaD1 }, never, DefaultArgs>

export default (d1: D1Database) => {
  if (!prisma) {
    const adapter = new PrismaD1(d1, 'true')
    prisma = new PrismaClient({ 
      adapter,
    })
  }
  return prisma
}

Environment & setup

  • OS: Cloudflare pages+worker (wrangler deploy)
  • Stack: Remix
  • Database: Cloudlare D1
  • Node.js version: 20

Prisma Version

prisma                  : 5.11.0
@prisma/client          : 5.11.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v20.11.1
Query Engine (Node-API) : libquery-engine efd2449663b3d73d637ea1fd226bafbcf45b3102 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli efd2449663b3d73d637ea1fd226bafbcf45b3102 (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.11.0-15.efd2449663b3d73d637ea1fd226bafbcf45b3102
Default Engines Hash    : efd2449663b3d73d637ea1fd226bafbcf45b3102
Studio                  : 0.499.0
Preview Features        : driverAdapters
@brunocascio brunocascio added the kind/bug A reported bug. label Mar 13, 2024
@Jolg42 Jolg42 self-assigned this Mar 13, 2024
@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: dates / DateTime domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: driverAdapters topic: @prisma/adapter-d1 labels Mar 13, 2024
@Jolg42
Copy link
Contributor

Jolg42 commented Mar 13, 2024

@brunocascio I'm curious to know what data you have in your database and what's the SQL you executed to create the database schema.

Because I could not reproduce this 🤔

See https://d1-tutorial.prisma.workers.dev/
I tried with the following on a Worker (I could try again with Pages, but that should technically be the same)

		await prisma.example.create({
			data: {},
		});
		const result = await prisma.example.findMany();
		console.debug(result);
-- CreateTable
CREATE TABLE "Example" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);
# locally
rm ./schema.sql && npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prisma --script > ./schema.sql
npx wrangler d1 execute DB --local --file=./schema.sql
wrangler dev

# and remotely
npx wrangler d1 execute DB --remote --file=./schema.sql
wrangler deploy

@Jolg42
Copy link
Contributor

Jolg42 commented Mar 13, 2024

Here is the data from D1 interface
Screenshot 2024-03-13 at 18 44 59

@brunocascio
Copy link
Author

brunocascio commented Mar 13, 2024

Thanks @Jolg42 for taking a look into it!

Aparrently I had an issue with the way I was seeding the database. So by moving the seed script I've built to an endpoint (for testing purposes) seems to solve the problem. Now is working correctly!

Thanks!

EDIT: Here's my data before solving the issue 🤦 (datetime was stored as integers rather than datetimes)

image

@Jolg42
Copy link
Contributor

Jolg42 commented Mar 14, 2024

@brunocascio Nice! I want to understand something here, how did you have integers here?

I'm curious to know what was the seed script you used for example.

@brunocascio
Copy link
Author

brunocascio commented Mar 14, 2024 via email

@Jolg42
Copy link
Contributor

Jolg42 commented Mar 14, 2024

@brunocascio Ok, but what was the code doing? Using the D1 adapter & prisma.model.create(.... date: new Date() ....)?

Or using the D1 database API directly, maybe?

@brunocascio
Copy link
Author

brunocascio commented Mar 14, 2024

@brunocascio Ok, but what was the code doing? Using the D1 adapter & prisma.model.create(.... date: new Date() ....)?

Or using the D1 database API directly, maybe?

I was not able to use prisma.model.create because didn't get how to do it with prisma mgirate/seed commands. I basically run it locally and then upload the data by using the d1 --import.

Sorry, it was like 2 weeks ago and didn't versionate that part so I lost the test script :(

So, don't think it was an issue with prisma/adapter itself

@Jolg42
Copy link
Contributor

Jolg42 commented Mar 14, 2024

Thanks @brunocascio! I was thinking something along these lines, but I wanted to check with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: dates / DateTime topic: driverAdapters topic: @prisma/adapter-d1 topic: sqlite
Projects
None yet
Development

No branches or pull requests

3 participants