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

TypeError: Do not know how to serialize a BigInt #816

Open
kakserpom opened this issue Dec 26, 2021 · 9 comments
Open

TypeError: Do not know how to serialize a BigInt #816

kakserpom opened this issue Dec 26, 2021 · 9 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. topic: BigInt topic: Do not know how to serialize a BigInt

Comments

@kakserpom
Copy link

  1. Prisma version (prisma -v or npx prisma -v):
    3.6.0
  2. Logs from Developer Tools Console or Command line, if any:
    TypeError: Do not know how to serialize a BigInt
    at JSON.stringify ()
    at new Tl (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/index.js:1:216912)
    at bs (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:27:50128)
    at Ha (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:27:67591)
    at bl (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:27:105796)
    at gu (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:27:97229)
    at hu (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:27:97154)
    at iu (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:27:94183)
    at file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:27:45779
    at e.unstable_runWithPriority (file:///Applications/Prisma%20Studio.app/Contents/Resources/app.asar/dist/renderer/assets/vendor.js:18:3854)

Please add:

BigInt.prototype.toJSON = function () {return this.toString()}
@sdnts
Copy link
Contributor

sdnts commented Dec 28, 2021

Hey @kakserpom, this was fixed in Prisma 3.7.0. If you update your Prisma version (or your electron app), this error should go away. Let me know if that's not the case, and I'll reopen!

@sdnts sdnts closed this as completed Dec 28, 2021
@kakserpom
Copy link
Author

image
@madebysid

@sdnts sdnts reopened this Dec 28, 2021
@sdnts
Copy link
Contributor

sdnts commented Jan 6, 2022

Hey, could you try out the latest Prisma CLI and let me know if this is fixed on there?

I haven't released a new version of the Electron app yet, but I will do that with the next release (this Tuesday). If you could confirm that this is fixed for you on the CLI, then it'll be fixed in the Electron app as well (they use the same code).

Let me know!

@aetheryx
Copy link

aetheryx commented Jan 11, 2022

@madebysid I am still seeing this using the Prisma CLI on Prisma v3.8.0:

TypeError: Do not know how to serialize a BigInt
at JSON.stringify ()
at new er (http://localhost:5555/assets/index.js:1:162604)
at bs (http://localhost:5555/assets/vendor.js:27:50128)
at Ha (http://localhost:5555/assets/vendor.js:27:67591)
at bl (http://localhost:5555/assets/vendor.js:27:105796)
at gu (http://localhost:5555/assets/vendor.js:27:97229)
at hu (http://localhost:5555/assets/vendor.js:27:97154)
at iu (http://localhost:5555/assets/vendor.js:27:94183)
at http://localhost:5555/assets/vendor.js:27:45779
at e.unstable_runWithPriority (http://localhost:5555/assets/vendor.js:18:3854)

$ npx prisma -v
Environment variables loaded from .env
prisma : 3.8.0
@prisma/client : 3.8.0
Current platform : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine : migration-engine-cli 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine : introspection-core 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary : prisma-fmt 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash : 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f
Studio : 0.452.0

@sdnts
Copy link
Contributor

sdnts commented Jan 11, 2022

Hmm, I can't seem to reproduce this, what are you doing exactly that causes this?

Here's what I did:

  1. Used a schema with a BigInt field (in Postgres)
  2. Opened up Studio via the CLI
  3. Created a new record & updated an existing one

@aetheryx
Copy link

aetheryx commented Jan 11, 2022

Yeah I'm doing something different - I see the bug when looking at a row's relations.

Schema:

model User {
  id    BigInt @id
  posts Post[]
}

model Post {
  id     BigInt @default(autoincrement())
  userID BigInt
  user   User   @relation(fields: [userID], references: [id])

  @@id([userID, id])
}

Steps:

  1. npx prisma studio
  2. Create a User row and save it
  3. Click the 0 Post cell:
    image

@sdnts
Copy link
Contributor

sdnts commented Jan 11, 2022

Ah, I see it now as well, weird. It only seems to happen when the ID is a BigInt. Nevertheless, I'll look into this, thanks for the repro!

@LB22 LB22 mentioned this issue Mar 15, 2022
@janpio janpio added kind/bug A reported bug. topic: BigInt labels Mar 21, 2022
@thojansen
Copy link

I ran into the same issue and created my own prisma studio launch script by using the studio-server. It almost works, but I now get a Value must be a valid BigInt error. So I guess simply adding the toJSON and fromJSON will not do the trick completely.

Screenshot 2022-04-22 at 08 19 16

import { enginesVersion } from '@prisma/engines';
import { getSchemaPathAndPrint } from '@prisma/migrate';
import { StudioServer } from '@prisma/studio-server';
import { loadEnvFile } from '@prisma/sdk'
import path from 'path';
import { logger } from './shared/logger';

const packageJson = require('../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires
const port = 5555;
const schemaPath = 'prisma/schema.prisma';

declare global
{
    interface BigInt
    {
        toJSON: () => number;
        fromJSON: () => BigInt;
    }
}

// eslint-disable-next-line no-extend-native
BigInt.prototype.toJSON = function () {
  const int = Number.parseInt(this.toString());
  return int ?? this.toString();
};

// eslint-disable-next-line no-extend-native
BigInt.prototype.fromJSON = function () {
  return BigInt(this);
};

const main = async (): Promise<void> => {
  await loadEnvFile(schemaPath, true);
  await getSchemaPathAndPrint();
  const staticAssetDir = path.resolve(__dirname, '../node_modules/@prisma/studio/dist');
  const studio = new StudioServer({
    schemaPath,
    port,
    staticAssetDir,
    versions: {
      prisma: packageJson.version,
      queryEngine: enginesVersion,
    },
  })

  await studio.start();
}

main().then(() => {
  const serverUrl = `http://localhost:${port}`;
  logger.info(`Prisma Studio is up on ${serverUrl}`);
}).catch((err) => logger.error(err));

I think it would be awesome if you could open source prisma studio at some point.

@kyranet
Copy link

kyranet commented May 18, 2022

Ah, I see it now as well, weird. It only seems to happen when the ID is a BigInt. Nevertheless, I'll look into this, thanks for the repro!

Not entirely true, consider this schema:

model Guild {
  id                 BigInt       @id
  channel            BigInt?
  addThread          Boolean      @default(false)
  addButtons         Boolean      @default(true)
  addUpdateHistory   Boolean      @default(false)
  useEmbed           Boolean      @default(true)
  suggestions        Suggestion[]
}

The studio is able to render them just fine:

image

However, this one schema does not:

model Suggestion {
  id         Int
  guild      Guild     @relation(fields: [guildId], references: [id])
  guildId    BigInt
  authorId   BigInt
  messageId  BigInt
  createdAt  DateTime  @default(now())
  repliedAt  DateTime?
  archivedAt DateTime?

  @@id([id, guildId])
}

And will result on a non-recoverable error:

Screenshot

image


A less hacky alternative to serialize bigints would be to use JSON.stringify's replacer argument. For example, this code is able to serialize BigInts just fine:

JSON.stringify({ id: 42n }, (key, value) => (typeof value === 'bigint' ? value.toString() : value));

Results on:

{"id":"42"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. topic: BigInt topic: Do not know how to serialize a BigInt
Projects
None yet
Development

No branches or pull requests

6 participants