Postgres Single Quote Escaping Breaking Migrations
See original GitHub issueBug description
Prisma Migrate does not handle double single quotes (‘’) in a schema. This is an issues because Postgres uses double single quotes (‘’) to escape a single quote (') in a JSONB default.
How to reproduce
- Create a schema.prisma file or pull from a db with the following schema (this is from an introspection on my specific DB; Notice the two quotes (‘’)):
birthday_award Json? @default("{\"amount\": 250, \"status\": false, \"message\": \"HAPPY BIRTHDAY, @receiver_username!! Here''s +amount to celebrate!\", \"visibility\": \"Publicly Visible\"}")
- Run
npx prisma migrate dev --name init
- Notice the error.
Expected behavior
The migration to successfully apply.
Prisma information
The line breaking the migration:
birthday_award Json? @default("{\"amount\": 250, \"status\": false, \"message\": \"HAPPY BIRTHDAY, @receiver_username!! Here''s +amount to celebrate!\", \"visibility\": \"Publicly Visible\"}")
The error given:
Applying migration `20220228230046_init`
Error: P3018
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 20220228230046_init
Database error code: 42601
Database error:
ERROR: syntax error at or near "\"
Position:
152 "require_increments_ten" BOOLEAN DEFAULT false,
153 "require_points" BOOLEAN DEFAULT true,
154 "reward_redemption_enabled" BOOLEAN DEFAULT false,
155 "allow_user_points" BOOLEAN DEFAULT true,
156 "email_sending_enabled" BOOLEAN DEFAULT true,
157 "birthday_award" JSONB DEFAULT '{"amount": 250, "status": false, "message": "HAPPY BIRTHDAY, @receiver_username!! Here\'\'s +amount to celebrate!", "visibility": "Publicly Visible"}',
DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState("42601"), message: "syntax error at or near \"\\\"", detail: None, hint: None, position: Some(Original(5069)), where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("scan.l"), line: Some(1180), routine: Some("scanner_yyerror") }
Environment & setup
- OS: MacOS Monterey 12.2.1
- Database: PostgreSQL accessed with pgAdmin 4
- Node.js version: v14.19.0
Prisma Version
Environment variables loaded from .env
prisma : 3.10.0
@prisma/client : 3.10.0
Current platform : darwin
Query Engine (Node-API) : libquery-engine 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 73e60b76d394f8d37d8ebd1f8918c79029f0db86
Studio : 0.458.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
PostgreSQL escape single quote | Basic Syntax of ... - eduCBA
Basically in PostgreSQL single quote is used to define string constant when a string has a single quote at that time you need...
Read more >Postgresql escape single quote in where clause [duplicate]
Single quotes go around string literals in Postgres. To escape a single quote inside your string literal, use two single quotes: 'John''s'.
Read more >Documentation: 15: 4.1. Lexical Structure - PostgreSQL
The escape character can be any single character other than a hexadecimal digit, the plus sign, a single quote, a double quote, or...
Read more >Release Notes - Flyway by Redgate • Database Migrations ...
Bug fixes. Issue 1909 Flyway breaks when JDBC driver returns null URL; Issue 2035 PostgreSQL: Restore role to its original value between migrations...
Read more >Don't use double quotes in PostgreSQL - Reuven Lerner
Now, there is a way around this, namely by using double quotes. Whereas single quotes in PostgreSQL are used to create a text...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you, this is the correct assumption and the correct bug. Thank you for looking into it.
Another bump here. We could really use this for our migrations. Also, workarounds are acceptable.