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.

Json defaults not parsed correctly

See original GitHub issue

Bug description

When trying to set a deafult in a json field it will output the deafult in the migration.sql file with out ’ and thus it will throw an error when trying to migrate because mysql doesn’t know what to do with the deafult value this also only seems to be a problem as of 3.7.0 because in 3.6.0 it was working as expected for me

How to reproduce

  1. Create a model with a Json field that has a deafult value (either array or object both is broken)
  2. Run prisma migrate dev --name test
  3. The error will be thrown

Expected behavior

I expect it to create a migration and migrate successfully applying the defaults in the process

Prisma information

My models that use the json field with defaults

model Members {
  id           Int           @id @default(autoincrement())
  guild        String        @db.VarChar(19)
  inviter      String?       @db.VarChar(19)
  user         String        @db.VarChar(19)
  inviteCount  Json          @default("{\"regular\":0,\"left\":0,\"fake\":0,\"bonus\":0}")
  GuildConfigs GuildConfigs? @relation(fields: [guild], references: [guild])
}

model Suggestions {
  id           Int              @id @default(autoincrement())
  guild        String           @db.VarChar(19)
  channel      String           @db.VarChar(19)
  message      String           @db.VarChar(19)
  upVoters     Json             @default("[]")
  downVoters   Json             @default("[]")
  status       SuggestionStatus
  proposer     String           @db.VarChar(19)
  suggestion   String           @db.Text
  GuildConfigs GuildConfigs     @relation(fields: [guild], references: [guild])
}

Environment & setup

  • OS: Windows 10
  • Database: Mysql
  • Node.js version: 16.6.1

Prisma Version

prisma                  : 3.7.0
@prisma/client          : 3.7.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio                  : 0.445.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Jarco-devcommented, Jan 6, 2022

Ok, after an internal conversation I know more:

Re Introspection, Json and Longtext: MariaDB surprisingly does not really support JSON as a data type, but maps it to LONGTEXT under the hood: https://mariadb.com/kb/en/json-data-type/ This might lead to unexpected behavior when introspecting a MariaDB database with Prisma. We could add handling of this to Re-Introspection (when you already have a schema with Json as the Prisma scalar type), but there are other cases where this would not help to get Json into your schema (no schema).

Re what you are experiencing: This is a regression of a regression unfortunately that affects MariaDB and MySQL in specific circumstances. We will try to fix this soon and handle this in a way that works consistently. You can probably use dbgenerated() in the meantime: https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#dbgenerated Instead of having @default("[]") try using @default(dbgenerated("[]"))

Thanks for taking your time to look at it, and I’ve already switched back to v3.6.0 for the time being as the things that were fixed in 3.7.0 didn’t affect me so I’ll be using 3.6.0 till there is a fix available.

0reactions
janpiocommented, Jan 5, 2022

Ok, after an internal conversation I know more:

Re Introspection, Json and Longtext: MariaDB surprisingly does not really support JSON as a data type, but maps it to LONGTEXT under the hood: https://mariadb.com/kb/en/json-data-type/ This might lead to unexpected behavior when introspecting a MariaDB database with Prisma. We could add handling of this to Re-Introspection (when you already have a schema with Json as the Prisma scalar type), but there are other cases where this would not help to get Json into your schema (no schema).

Re what you are experiencing: This is a regression of a regression unfortunately that affects MariaDB and MySQL in specific circumstances. We will try to fix this soon and handle this in a way that works consistently. You can probably use dbgenerated() in the meantime: https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#dbgenerated Instead of having @default("[]") try using @default(dbgenerated("[]"))

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is this JSON not parsing correctly? - Stack Overflow
1 Answer 1. Sorted by: Reset to default. Highest score (default) ...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >
JSON parameters are not parsed correctly using CMD #2680
JSON parameters are not parsed correctly using CMD #2680 ... It seems that @settings.json is working fine, but it's a lot more annoying...
Read more >
JSON message is not parsed - New Relic Documentation
When JSON content is sent in the log's message field, it's not automatically parsed, and it's not stored as attributes (key/value pairs).
Read more >
parse a prettified JSON - CDAP Documentation - Confluence
This article describes how to build a pipeline to parse a prettified JSON file. Background. By default, the sources are configured with Text...
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