prisma / prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
https://www.prisma.io
Apache License 2.0
38.97k stars 1.53k forks source link

MongoDB embedding shows a wrong error type #13901

Open indicozy opened 2 years ago

indicozy commented 2 years ago

Bug description

I've decided to try embedding mongodb documents with the new update, however, when the transaction cannot pass the error shows the wrong error type. The error was in the different type of mutation data and the schema's type, however, the error shows that it cannot find the document that I'm trying to create.

How to reproduce

  1. Install prisma with mongodb with the schema described below.
  2. Create new document with mutation:
    prisma.submission.create(
    {
    data: {
    studentId: '62af1f5934486d50e3167535',
    blockId: '62af1f05f03ee66298116424',
    data: {
      dataCode: {
      ~~~~~~~~
        code: 'x = 10',
        codeOutput: {
          stdout: '10\n',
          stderr: '',
          status: 1 // here it should be true, not 1
        }
      }
    },
    success: true
    }
    }
    )
  3. Get error:
    
    error - Error: 
    Invalid `prisma.submission.create()` invocation:
    {
    data: {
    studentId: '62af1f5934486d50e3167535',
    blockId: '62af1f05f03ee66298116424',
    data: {
      dataCode: {
      ~~~~~~~~
        code: 'x = 10',
        codeOutput: {
          stdout: '10\n',
          stderr: '',
          status: 1 // here it should be true, not 1
        }
      }
    },
    success: true
    }
    }

Unknown arg dataCode in data.data.dataCode for type SubmissionDataCreateEnvelopeInput. Did you mean set? Available args: type SubmissionDataCreateEnvelopeInput { set?: SubmissionDataCreateInput }

4. If the type of the `status` is the same as in schema (`Boolean`), everything works fine.

### Expected behavior

It should give a different error, not `Unknown arg 'dataCode' in ...`. It should to be that the variable has a different type in the embedded document.

### Prisma information

<!-- Do not include your database credentials when sharing your Prisma schema! -->
## Prisma library:
```js
import { PrismaClient } from '@prisma/client';

// eslint-disable-next-line import/no-mutable-exports
let prisma;

if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient();
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient();
  }
  prisma = global.prisma;
}

// Some non-related middleware

export default prisma;

Schema snippet:

model Submission {
  id        String         @id @default(auto()) @map("_id") @db.ObjectId
//  createdAt DateTime       @default(now())
//  updatedAt DateTime       @updatedAt
//  student   Student        @relation(fields: [studentId], references: [id])
//  studentId String         @db.ObjectId
//  block     Block          @relation(fields: [blockId], references: [id])
//  blockId   String         @db.ObjectId
  data      SubmissionData
  success   Boolean

//  @@unique([studentId, blockId])
}

type SubmissionData {
  dataQuiz SubmissionQuiz?
  dataCode SubmissionCode?
}

type SubmissionQuiz {
  guess Json
}

type SubmissionCode {
  code       String
  codeOutput CodeOutput
}

unused 'columns' were commented out.

Environment & setup

Prisma Version

prisma                  : 3.15.1
@prisma/client          : 3.15.1
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/.pnpm/@prisma+engines@3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e/node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/.pnpm/@prisma+engines@3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/.pnpm/@prisma+engines@3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/.pnpm/@prisma+engines@3.15.1-1.461d6a05159055555eb7dfb337c9fb271cbd4d7e/node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 461d6a05159055555eb7dfb337c9fb271cbd4d7e
Studio                  : 0.462.0
rossinicolas commented 2 years ago

Hi guys, i have a similar issue. In my prisma schema definition, i have a model that has an embedded document as field using type definition. That type definition has a field like String[]:

model myModel {
 id             String                          @id @default(auto()) @map("_id") @db.ObjectId
info        MyType
}

type MyType {
field         String[]
}

so, in playgroud when i try to create a new MyModel Document i have to use a mutation similar to this:

mutation {
createMyModel(
data:{
info:{set:["s1"]}
})
}

but, prisma show me this error message:

The current database provider doesn't support a feature that the query used: Unhandled and unsupported value mapping for MongoDB: { \"set\": [String(\"s1\")] } as String."

if i change the field type from String[] to Json there aren't problem. I really appreciate your help

rossinicolas commented 2 years ago

if i change the MyType definition to

type MyType {
field Json
}

i don't have any problems but that's not an option if you will has a typed code

janpio commented 1 year ago

Next step: Reconfirm bug again with most recent Prisma version, compare error message according to https://github.com/prisma/team-orm/issues/222.

saalihou commented 11 months ago

Hi guys, i have a similar issue. In my prisma schema definition, i have a model that has an embedded document as field using type definition. That type definition has a field like String[]:

model myModel {
 id             String                          @id @default(auto()) @map("_id") @db.ObjectId
info        MyType
}

type MyType {
field         String[]
}

so, in playgroud when i try to create a new MyModel Document i have to use a mutation similar to this:

mutation {
createMyModel(
data:{
info:{set:["s1"]}
})
}

but, prisma show me this error message:

The current database provider doesn't support a feature that the query used: Unhandled and unsupported value mapping for MongoDB: { \"set\": [String(\"s1\")] } as String."

if i change the field type from String[] to Json there aren't problem. I really appreciate your help

I am able to reproduce this issue using Prisma 5.4.2. Any updates or workarounds ? This makes it impossible to use embedded arrays altogether with the MongoDB connector