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.

Postgres Single Quote Escaping Breaking Migrations

See original GitHub issue

Bug 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

  1. 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\"}")
  1. Run npx prisma migrate dev --name init
  2. 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:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ajhollowayvrmcommented, Mar 16, 2022

Update: I assumed that the original table should say Here's and modified the generated SQL to match that:

CREATE TABLE "Foo" (
    "id" INTEGER NOT NULL,
    "bar" JSONB DEFAULT '{"message": "This message includes a quote: Here''s it!"}',

    CONSTRAINT "Foo_pkey" PRIMARY KEY ("id")
);

When you create a new row of that table, you get a Here's via the default value. But when you db pull, it will write the schema we have above, and it will then on migrate dev again create broken SQL instead of the SQL of the original table.

Thank you, this is the correct assumption and the correct bug. Thank you for looking into it.

0reactions
ajhollowayvrmcommented, May 12, 2022

Another bump here. We could really use this for our migrations. Also, workarounds are acceptable.

Read more comments on GitHub >

github_iconTop 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 >

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