studio icon indicating copy to clipboard operation
studio copied to clipboard

TypeError: Do not know how to serialize a BigInt

Open kakserpom opened this issue 4 years ago • 9 comments

  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()}

kakserpom avatar Dec 26 '21 01:12 kakserpom

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 avatar Dec 28 '21 13:12 sdnts

image @madebysid

kakserpom avatar Dec 28 '21 16:12 kakserpom

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!

sdnts avatar Jan 06 '22 12:01 sdnts

@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

aetheryx avatar Jan 11 '22 15:01 aetheryx

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

sdnts avatar Jan 11 '22 16:01 sdnts

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

aetheryx avatar Jan 11 '22 16:01 aetheryx

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!

sdnts avatar Jan 11 '22 17:01 sdnts

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.

thojansen avatar Apr 22 '22 06:04 thojansen

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"}

kyranet avatar May 18 '22 21:05 kyranet

Hey :wave:

Prisma ORM 7 introduces a brand new standalone Studio, built from the ground up to work everywhere, with or without Prisma ORM. Consequently, all "old Studio" issues are being closed as there won't be any further work on it. :mega:

The new Studio is also available in the Prisma VS Code extension and in the Prisma Console application. :electric_plug:

Help shape, improve and fix the new Studio with us in this repository by submitting bug reports and feature requests. :construction_worker:

igalklebanov avatar Nov 20 '25 01:11 igalklebanov