question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attempted to serialize scalar 'null' with incompatible type 'String' for field mainPhotoId.

See original GitHub issue

Bug description

Hello, when I’m trying to fetch an entity (Album) from following schema, the error occurs.

Attempted to serialize scalar 'null' with incompatible type 'String' for field mainPhotoId.

this.db.album.findUnique({ where: { id: albumId } });
// db is PrismaClient instance

How to reproduce

  1. Clone the repository https://github.com/zuffik/vivse-be
  2. run with docker-compose up app-dev
  3. when server starts try to execute request from following postman export (use any image file for upload) Playground.postman_collection.json.txt (change .txt to .json, GH does not allow to upload json)
  4. you should see this error image

Expected behavior

It should return the album entity normally.

Prisma information

generator client {
  provider      = "prisma-client-js"
  output        = "../prisma/generated/client"
  binaryTargets = ["native", "darwin", "debian-openssl-1.1.x", "linux-musl"]
}

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

model User {
  id       String  @id @default(uuid())
  email    String  @unique
  name     String?
  password String
}

model Album {
  id          String   @id @default(uuid())
  createdAt   DateTime @default(now())
  label       String
  mainPhotoId String?
  main        Photo?   @relation("MainPhoto", fields: [mainPhotoId], references: [id])
  photos      Photo[]  @relation("Photos")
}

model ImageFile {
  id          String   @id @default(uuid())
  createdAt   DateTime @default(now())
  original    String
  small       String
  large       String
  editedFor   Photo[]  @relation("EditedFile")
  originalFor Photo[]  @relation("OriginalFile")
}

model Photo {
  id             String     @id @default(uuid())
  createdAt      DateTime   @default(now())
  originalFileId String
  editedFileId   String?
  albumId        String
  label          String
  album          Album      @relation("Photos", fields: [albumId], references: [id])
  editedFile     ImageFile? @relation("EditedFile", fields: [editedFileId], references: [id])
  originalFile   ImageFile  @relation("OriginalFile", fields: [originalFileId], references: [id])
  mainFor        Album[]    @relation("MainPhoto")
}

model Url {
  token     String   @id
  createdAt DateTime @default(now())
  expiresAt DateTime
  type      UrlType
}

enum UrlType {
  PICK
  DOWNLOAD
}

Environment & setup

  • OS: MacOS host with alpine docker image
  • Database: postgres:14
  • Node.js version: 16.13.2

Prisma Version

3.9.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zuffikcommented, Oct 19, 2022

Hi. Sorry for not noticing the comments. In the meantime I updated the schema with another solution that was acceptable to my use case. I tried to test also the scheme attached to this issue but everything is working fine, so I suppose you can close the issue.

1reaction
zuffikcommented, Feb 14, 2022

Hello here is the output:

Mon, 14 Feb 2022 17:07:50 GMT express:router dispatching POST /album/bcfdc976-6cca-4998-a1b8-d5b03b25a9a5/upload
2022-02-14T17:07:50.130306000Z Mon, 14 Feb 2022 17:07:50 GMT express:router query  : /album/bcfdc976-6cca-4998-a1b8-d5b03b25a9a5/upload
2022-02-14T17:07:50.130809700Z Mon, 14 Feb 2022 17:07:50 GMT express:router expressInit  : /album/bcfdc976-6cca-4998-a1b8-d5b03b25a9a5/upload
2022-02-14T17:07:50.131483000Z Mon, 14 Feb 2022 17:07:50 GMT express:router jsonParser  : /album/bcfdc976-6cca-4998-a1b8-d5b03b25a9a5/upload
2022-02-14T17:07:50.132412900Z Mon, 14 Feb 2022 17:07:50 GMT body-parser:json content-type "multipart/form-data; boundary=--------------------------919840002605483545984588"
2022-02-14T17:07:50.135347000Z Mon, 14 Feb 2022 17:07:50 GMT body-parser:json skip parsing
2022-02-14T17:07:50.135401400Z Mon, 14 Feb 2022 17:07:50 GMT express:router urlencodedParser  : /album/bcfdc976-6cca-4998-a1b8-d5b03b25a9a5/upload
2022-02-14T17:07:50.135517900Z Mon, 14 Feb 2022 17:07:50 GMT body-parser:urlencoded content-type "multipart/form-data; boundary=--------------------------919840002605483545984588"
2022-02-14T17:07:50.135846100Z Mon, 14 Feb 2022 17:07:50 GMT body-parser:urlencoded skip parsing
2022-02-14T17:07:50.204846600Z 2022-02-14T17:07:50.204Z prisma:client Prisma Client call:
2022-02-14T17:07:50.208278700Z 2022-02-14T17:07:50.208Z prisma:client prisma.album.findUnique({
2022-02-14T17:07:50.208328500Z   where: {
2022-02-14T17:07:50.208343900Z     id: 'bcfdc976-6cca-4998-a1b8-d5b03b25a9a5'
2022-02-14T17:07:50.208356600Z   }
2022-02-14T17:07:50.208366900Z })
2022-02-14T17:07:50.208379200Z 2022-02-14T17:07:50.208Z prisma:client Generated request:
2022-02-14T17:07:50.208391200Z 2022-02-14T17:07:50.208Z prisma:client query {
2022-02-14T17:07:50.208403300Z   findUniqueAlbum(where: {
2022-02-14T17:07:50.208415800Z     id: "bcfdc976-6cca-4998-a1b8-d5b03b25a9a5"
2022-02-14T17:07:50.208428500Z   }) {
2022-02-14T17:07:50.208440800Z     id
2022-02-14T17:07:50.208450900Z     createdAt
2022-02-14T17:07:50.208462500Z     label
2022-02-14T17:07:50.208472800Z     mainPhotoId
2022-02-14T17:07:50.208484900Z   }
2022-02-14T17:07:50.208496800Z }
2022-02-14T17:07:50.208508800Z 
2022-02-14T17:07:50.209697800Z 2022-02-14T17:07:50.209Z prisma:client:libraryEngine sending request, this.libraryStarted: true
2022-02-14T17:07:50.219371300Z prisma:query SELECT 1
2022-02-14T17:07:50.239781600Z prisma:query SELECT "public"."Album"."id", "public"."Album"."createdAt", "public"."Album"."label", "public"."Album"."mainPhotoId" FROM "public"."Album" WHERE "public"."Album"."id" = $1 LIMIT $2 OFFSET $3
2022-02-14T17:07:50.240832500Z 2022-02-14T17:07:50.240Z prisma:client:fetcher Error: Attempted to serialize scalar 'null' with incompatible type 'String' for field mainPhotoId.
2022-02-14T17:07:50.240869300Z     at prismaGraphQLToJSError (/usr/src/app/prisma/generated/client/runtime/index.js:35994:10)
2022-02-14T17:07:50.240880700Z     at Object.request (/usr/src/app/prisma/generated/client/runtime/index.js:36358:17)
2022-02-14T17:07:50.262119800Z [Nest] 73  - 02/14/2022, 5:07:50 PM   ERROR [ExceptionsHandler] 
2022-02-14T17:07:50.262172000Z Invalid `prisma.album.findUnique()` invocation:
2022-02-14T17:07:50.262185100Z 
2022-02-14T17:07:50.262198900Z 
2022-02-14T17:07:50.262211400Z   Attempted to serialize scalar 'null' with incompatible type 'String' for field mainPhotoId.
2022-02-14T17:07:50.262276700Z Error: 
2022-02-14T17:07:50.262292200Z Invalid `prisma.album.findUnique()` invocation:
2022-02-14T17:07:50.262331500Z 
2022-02-14T17:07:50.262343800Z 
2022-02-14T17:07:50.262366600Z   Attempted to serialize scalar 'null' with incompatible type 'String' for field mainPhotoId.
2022-02-14T17:07:50.262379400Z     at cb (/usr/src/app/prisma/generated/client/runtime/index.js:38707:17)
2022-02-14T17:07:50.262392000Z     at DbService._request (/usr/src/app/prisma/generated/client/runtime/index.js:40859:18)
2022-02-14T17:07:50.262405400Z     at AlbumController.checkAlbumAndStoreFile (/usr/src/app/src/picture/album/album.controller.ts:34:19)
2022-02-14T17:07:50.262416400Z     at AlbumController.createPhoto (/usr/src/app/src/picture/album/album.controller.ts:52:38)

When executing the query

SELECT "public"."Album"."id",
       "public"."Album"."createdAt",
       "public"."Album"."label",
       "public"."Album"."mainPhotoId"
FROM "public"."Album"
WHERE "public"."Album"."id" = 'bcfdc976-6cca-4998-a1b8-d5b03b25a9a5'
LIMIT 1 OFFSET 0

the record is found:

image
Read more comments on GitHub >

github_iconTop Results From Across the Web

Attempted to serialize scalar 'null' with incompatible type 'String'
Bug description I'm gettingAttempted to serialize scalar 'null' with incompatible type 'String' when trying to create new records that use ...
Read more >
Compiler error with Prisma - typescript - Stack Overflow
I'm preparing a project with GraphQL server, using Prisma to connect with the database, all this with Typscript . And the compiler is...
Read more >
prisma - bytemeta
Set foreign keys directly with a self relation field ... Attempted to serialize scalar 'null' with incompatible type 'String' for field mainPhotoId.
Read more >
prisma - `findMany` with a `select` argument errors out with ...
(Note that the User field was automatically added by the VSCode ... findMany(Attempted to serialize scalar 'null' with incompatible type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found