Skip to content

Commit

Permalink
fix(generator): adapt unique field casing (#1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Dec 21, 2023
1 parent 5510c92 commit e97ad54
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion generator/ast/transform/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func indexes(m dmmf.Model) []Index {

if len(m.PrimaryKey.Fields) > 0 {
idx = append(idx, Index{
Name: getName(concatFieldsToName(m.PrimaryKey.Fields), m.PrimaryKey.Fields),
Name: convert(m.PrimaryKey.Fields),
InternalName: concatFieldsToName(m.PrimaryKey.Fields),
Fields: m.PrimaryKey.Fields,
})
Expand All @@ -51,6 +51,10 @@ func getName(field string, fields []types.String) types.String {
if field != "" {
return types.String(field)
}
return convert(fields)
}

func convert(fields []types.String) types.String {
var name string
for _, f := range fields {
name += f.GoCase()
Expand Down
2 changes: 1 addition & 1 deletion generator/ast/transform/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (m Model) CompoundKeys() []Index {

if m.OldModel.PrimaryKey.Name != "" {
items = append(items, Index{
Name: m.OldModel.PrimaryKey.Name,
Name: types.String(m.OldModel.PrimaryKey.Name.GoCase()),
InternalName: m.OldModel.PrimaryKey.Name.String(),
Fields: m.OldModel.PrimaryKey.Fields,
})
Expand Down
2 changes: 1 addition & 1 deletion generator/templates/query.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

{{/* composite keys for FindUnique */}}
{{ range $unique := $model.CompoundKeys }}
func ({{ $nsQuery }}) {{ $unique.Name.GoCase }}(
func ({{ $nsQuery }}) {{ $unique.Name }}(
{{- range $f := $unique.Fields }}
_{{- $f.GoLowerCase }} {{ $model.Name.GoCase }}WithPrisma{{ $f.GoCase }}WhereParam,
{{ end -}}
Expand Down
7 changes: 7 additions & 0 deletions test/features/composite/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type cx = context.Context
type Func func(t *testing.T, client *PrismaClient, ctx cx)

func TestComposite(t *testing.T) {
// no-op compile time test

User.SomethingIDAnotherIDStuff(
User.SomethingID.Equals(""),
User.AnotherIDStuff.Equals(""),
)

tests := []struct {
name string
before []string
Expand Down
4 changes: 4 additions & 0 deletions test/features/composite/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ model User {
middleName String
lastName String
somethingId String?
anotherIdStuff String?
@@id([firstName, lastName])
@@unique([firstName, middleName, lastName])
@@unique([somethingId, anotherIdStuff])
}

0 comments on commit e97ad54

Please sign in to comment.