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.

EPIC: Support constraints and indexes with names that have invalid characters in Javascript

See original GitHub issue

Problem

Right now if you have the following schema:

model A {
  a  Int
  b  Int
  @@unique([a, b], name: "no-dupes")
}

You’ll end up with a generated client with syntax errors. This is because we use the name of @@unique in our generated code. In Javascript, you can’t have identifiers with a dash (e.g. -).

This problem also applies to our other constraints like @@id and @@index. For @@index, we don’t currently expose the name to the client so it’s not a problem for us yet (and it may never be).

Suggested solution

Similar to our @map() at the field level, we could introduce:

@@unique([a, b], name: "noDupes", map: "no-dupes")

In this case name could be optional since its final form would be dependent on the language.

As for which characters should we add map to, I think most languages have a fairly standard concept of identifiers. We could allow names [A-Za-z][A-Za-z0-9]* without map, every other name gets a map.

Alternatives

It might also make sense to be more precise and investigate our usage of @@id, @@unique. If we’re using them as keys, we could get away with adding quotes around them. For example, { "no-dupes": ... } is valid Javascript syntax.

Another idea that would also solve this issue: https://github.com/prisma/prisma/issues/4010

Engine Tasks

References

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
janpiocommented, Oct 21, 2020

Issue https://github.com/prisma/prisma/issues/3964 reported the exact same problem and was closed as a duplicate of this - but includes a great real world example that came from just using Prisma.

0reactions
do4grcommented, Aug 11, 2021

This is now available via the namedConstraints preview flag since 2.29. See https://www.prisma.io/docs/guides/upgrade-guides/upgrading-to-use-preview-features/enabling-named-constraints

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: illegal character - JavaScript - MDN Web Docs
The JavaScript exception "illegal character" occurs when there is an invalid or unexpected token that doesn't belong at this position in the ...
Read more >
Restrictions and conventions for naming objects - Azure DevOps
Common restrictions include not exceeding the character length for a name, not containing special characters, and maintaining uniqueness of ...
Read more >
Adding Database Indexes - GitLab Docs
Considerations for index names. Check our Constraints naming conventions page. Why explicit names are required. As Rails is database agnostic, it generates an ......
Read more >
ERDDAP - Working with the datasets.xml File
Fortunately, ERDDAP comes with two Tools to help you create the XML for each dataset in ... If you need to enter a...
Read more >
All other Tokyo fixes - Product Documentation | ServiceNow
Special characters like underscores (_) are removed. ... Order Management for Customer Service Management release notes.
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