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

Improve error on default values when @db.ObjectId isn't present #6429

Closed
matthewmueller opened this issue Apr 6, 2021 · 4 comments
Closed
Assignees
Labels
kind/improvement An improvement to existing feature and code. team/schema Issue for team Schema. topic: mongodb topic: schema validation
Milestone

Comments

@matthewmueller
Copy link
Contributor

matthewmueller commented Apr 6, 2021

(MongoDB is not publicly available yet. This issue is for the development build.)

Problem

Given the following Prisma Schema

datasource db {
  provider = "mongodb"
  url      = "mongodb://localhost:27017/mydb"
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id    String @id @default(dbgenerated("")) @map("_id")
  email String @unique
}

This is a valid schema according to prisma validate:

$ prisma validate --schema client/fixtures/mongo/prisma/schema.prisma 
Prisma schema loaded from client/fixtures/mongo/prisma/schema.prisma
The schema at client/fixtures/mongo/prisma/schema.prisma is valid 🚀

Unfortunately, when you run the following:

import { PrismaClient } from './@prisma/client'

const client = new PrismaClient()

async function main() {
  console.time('connect')
  await client.$connect()
  console.timeEnd('connect')
  console.time('create')
  const user = await client.user.create({
    data: {
      email: 'max@gmail.com',
    },
  })
  console.log(user)
  console.timeEnd('create')
}

main()
  .catch(console.error)
  .finally(() => client.$disconnect())

You get the following error:

$ DEBUG=prisma:client ts-node index.ts 
  prisma:client clientVersion: local +0ms
connect: 149.264ms
  prisma:client Prisma Client call: +183ms
  prisma:client prisma.user.create({
  prisma:client   data: {
  prisma:client     email: 'max@gmail.com'
  prisma:client   }
  prisma:client }) +1ms
  prisma:client Generated request: +0ms
  prisma:client mutation {
  prisma:client   createOneUser(data: {
  prisma:client     email: "max@gmail.com"
  prisma:client   }) {
  prisma:client     id
  prisma:client     email
  prisma:client   }
  prisma:client }
  prisma:client  +0ms
  prisma:client Error: Invalid response data: the query result was required, but an empty Object((Weak)) was returned instead.
  prisma:client     at NodeEngine.graphQLToJSError (/Users/m/dev/src/github.com/prisma/prisma/src/packages/engine-core/src/NodeEngine.ts:1184:12)
  prisma:client     at NodeEngine.request (/Users/m/dev/src/github.com/prisma/prisma/src/packages/engine-core/src/NodeEngine.ts:963:22)
  prisma:client     at processTicksAndRejections (internal/process/task_queues.js:93:5)
  prisma:client     at cb (/Users/m/dev/src/github.com/prisma/prisma/src/packages/client/src/runtime/getPrismaClient.ts:1542:26)
  prisma:client     at main (/Users/m/dev/src/github.com/prisma/prisma/src/packages/client/fixtures/mongo/index.ts:10:16) +38ms
PrismaClientUnknownRequestError: 
Invalid `prisma.user.create()` invocation:


  Invalid response data: the query result was required, but an empty Object((Weak)) was returned instead.
    at cb (/Users/m/dev/src/github.com/prisma/prisma/src/packages/client/src/runtime/getPrismaClient.ts:1595:17)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at main (/Users/m/dev/src/github.com/prisma/prisma/src/packages/client/fixtures/mongo/index.ts:10:16) {
  clientVersion: 'local'
}

Suggested Solution

We validate that you need @db.ObjectId and don't allow a generated client in this state or provide a better error informing users to supply @db.ObjectId.

@matthewmueller matthewmueller added kind/improvement An improvement to existing feature and code. topic: mongodb labels Apr 6, 2021
@pantharshit00 pantharshit00 added the team/client Issue for team Client. label Apr 6, 2021
@matthewmueller matthewmueller added this to the 2.22.0 milestone Apr 14, 2021
@xdave
Copy link

xdave commented Apr 22, 2021

I've been using plain strings as primary keys ("_id") in mongodb for quite some time, so I would prefer if we didn't force the use of ObjectID. You can technically put anything you want in that field.

EDIT: Oh, I think this is for if you provided dbgenerated() as a default value? Yeah, then I'm OK with that, because I've been testing it out with uuid(), and it seems to work fine.

@dpetrick
Copy link
Contributor

At first I was unsure why that's the case, as in theory the ID get's returned on create just fine. However, the issue is not the create operation, it's the subsequent read required by the createOne operation. All the query core has after the create is for example a String of "608197d600f21c5d0085e524". Querying with that string to retrieve the record after the write completed will fail because the driver needs to know that it's a Bson ObjectID, not a string. Querying with a string will not return anything, because there's no document with a Bson::String ID.

That's a tough one to validate. The best one I can think of is to require a native type annotation on @id db generates, but that may have hidden edge cases.

@matthewmueller matthewmueller modified the milestones: 2.22.0, 2.23.0 May 5, 2021
@matthewmueller matthewmueller modified the milestones: 2.23.0, 2.24.0 May 19, 2021
@janpio janpio changed the title Improve error on default values when @db.ObjectId isn't present Improve error on default values when @db.ObjectId isn't present Jan 28, 2022
@janpio janpio added process/candidate team/schema Issue for team Schema. topic: schema validation and removed topic: error team/client Issue for team Client. labels Jan 28, 2022
@janpio
Copy link
Member

janpio commented Jan 28, 2022

Note: Unclear if this can really be validated as an error, or if a similar schema to OP could actually be a valid case:

model User {
  id    String @id @default("") @map("_id")
  email String @unique
}

As long as a custom id is provided on create, everything would probably be fine.

@janpio janpio modified the milestones: 2.24.0, 3.10.0 Feb 10, 2022
@janpio janpio assigned pimeys and unassigned dpetrick Feb 10, 2022
@pimeys
Copy link
Contributor

pimeys commented Feb 11, 2022

The schema in the original message validates:

2022-02-11_13-02-1644584394

So let's try again with auto():

2022-02-11_14-02-1644584420

This was fixed a while ago when we switched to auto() from dbgenerated().

@pimeys pimeys closed this as completed Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/improvement An improvement to existing feature and code. team/schema Issue for team Schema. topic: mongodb topic: schema validation
Projects
None yet
Development

No branches or pull requests

7 participants