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.

escaping \n inside TEXT field

See original GitHub issue

I have a text field that contains \n. When I access my data via Hasura, it adds extra slash to it resulting: \\n. I tried couple of ways to work around it like adding /\n but that resulted to /\\n. All I need to have my text exactly the way I inserted but I assume as you convert value to json friendly, it escapes. Any idea how I can work around it? (Not sure if it is intended or a bug)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lexi-lambdacommented, Aug 27, 2019

Ah—the issue you’re having is that Postgres does not support \n escapes in ordinary string literals (because SQL does not). So when you write

INSERT INTO test.mytable (title, description)
  VALUES ('title1', 'Some text bla bla bla \n\n and some other');

you’re actually inserting a string with the character sequence “backslash n backslash n” into your database, not a string with newlines. If you want to include newlines in string literals, you can use “‘escape’ string constants,” which are prefixed with the letter E. For example,

INSERT INTO test.mytable (title, description)
  VALUES ('title1', E'Some text bla bla bla \n\n and some other');

will do what you want.

0reactions
yogeshwar607commented, Sep 20, 2022

Ah—the issue you’re having is that Postgres does not support \n escapes in ordinary string literals (because SQL does not). So when you write

INSERT INTO test.mytable (title, description)
  VALUES ('title1', 'Some text bla bla bla \n\n and some other');

you’re actually inserting a string with the character sequence “backslash n backslash n” into your database, not a string with newlines. If you want to include newlines in string literals, you can use “‘escape’ string constants,” which are prefixed with the letter E. For example,

INSERT INTO test.mytable (title, description)
  VALUES ('title1', E'Some text bla bla bla \n\n and some other');

will do what you want.

How we do it in hasura query ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can you process a newline escape sequence "\n" in a ...
Newline sequences work fine in Text("hello\nworld") but will simply display the "\n" instead of a newline if used with TextField.
Read more >
Text Field Escape Codes - Documentation - Knowledge Base
Text fields marked with a console/terminal icon can contain backslash-style escape codes for encoding newlines, carriage returns, ...
Read more >
Preventing escaped characters in TextField - Unity Forum
I have a visual tree containing several TextField elements. One of these elements is intended to include a directory path.
Read more >
Escape Sequences
An escape sequence is a set of characters used to insert into a string certain ... Note: The \n escape sequence works only...
Read more >
' --> ' changes to ' --> ' inside a text field
Writing "-->" in a string field, corrupts the content with escaped HTML characters. If you enter in the text: '-->', for example in...
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