EPIC: Support constraints and indexes with names that have invalid characters in Javascript
See original GitHub issueProblem
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
- https://github.com/prisma/prisma-engines/issues/1037
- https://github.com/prisma/prisma-engines/issues/1038
- https://github.com/prisma/prisma-engines/issues/1039
- https://github.com/prisma/prisma-engines/issues/1040
References
- Community Issue: #2393.
- More discussion about the problem: https://prisma.slack.com/archives/CCWDULGUW/p1613539035068300
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:7 (7 by maintainers)
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.
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