Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting Escaped Code Example #5409

Merged
merged 1 commit into from
Nov 3, 2023
Merged

Correcting Escaped Code Example #5409

merged 1 commit into from
Nov 3, 2023

Conversation

shery
Copy link
Contributor

@shery shery commented Oct 31, 2023

In js, according to the normal string escape rules, need to add \ before \

In js, according to the normal string escape rules, need to add `\` before `\`
@vercel
Copy link

vercel bot commented Oct 31, 2023

@shery is attempting to deploy a commit to the Prisma Team on Vercel.

A member of the Team first needs to authorize it.

@ruheni ruheni requested a review from jharrell October 31, 2023 17:22
@jharrell
Copy link
Member

jharrell commented Nov 1, 2023

@shery Hey! Thanks for your PR. I think you might be mistaken. We're not trying to escape \ we're just trying to escape _ so \ should not be escaped.

To put it another way, the example shows that the underlying database will understand _benny and \_benny differently as the latter will treat _ as a literal character instead of a wildcard.

@shery
Copy link
Contributor Author

shery commented Nov 3, 2023

Hi @jharrell, If executing SQL statements through the mysql Client, then \_ will work as expected (matching the _ character). However, Prisma Client runs in a JavaScript environment, and due to JavaScript's string escape rules (escaping special characters with \), \_ will be interpreted as escaping _ in JavaScript string, so the actual in-memory character is _, causing the finally generated SQL query statement to not contain \. This results in _ not being escaped.

Here is a simple example:

id name age city
1 John Doe 35 new_york
2 Jane Smith 28 newxyork
const arr1 = await prisma.user.findMany({
  where: {
    city: {
      contains: 'new\_york',
    },
  },
})

console.log(arr1)
// [{"id":1,"name":"John Doe","age":35,"city":"new_york"},{"id":2,"name":"Jane Smith","age":28,"city":"newxyork"}]

const arr2 = await prisma.user.findMany({
  where: {
    city: {
      contains: 'new\\_york',
    },
  },
})

console.log(arr2)
// [{"id":1,"name":"John Doe","age":35,"city":"new_york"}]

@jharrell
Copy link
Member

jharrell commented Nov 3, 2023

@shery Huh. How about that. You are correct! Thank you for correcting and apologies for my mistake.

Copy link

vercel bot commented Nov 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 3, 2023 2:44pm

@jharrell jharrell merged commit 67add2f into prisma:main Nov 3, 2023
3 of 4 checks passed
@shery shery deleted the patch-1 branch November 5, 2023 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants