entc/gen: fix conflict between set_id edge field and mutation fields (#4062)

This commit is contained in:
Ariel Mashraki
2024-05-19 14:26:59 +03:00
committed by GitHub
parent e56fd86cad
commit 61751fc92c
71 changed files with 830 additions and 147 deletions

View File

@@ -127,11 +127,7 @@ func ({{ $receiver }} *{{ $builder }}) check() error {
{{- end }}
{{- range $e := $.EdgesWithID }}
{{- if not $e.Optional }}
{{- if $e.Unique }}
if _, ok := {{ $mutation }}.{{ $e.StructField }}ID(); !ok {
{{- else }}
if len({{ $mutation }}.{{ $e.StructField }}IDs()) == 0 {
{{- end }}
if len({{ $mutation }}.{{ $e.StructField }}IDs()) == 0 {
return &ValidationError{Name: "{{ $e.Name }}", err: errors.New(`{{ $pkg }}: missing required edge "{{ $.Name }}.{{ $e.Name }}"`)}
}
{{- end }}

View File

@@ -269,7 +269,7 @@ func ({{ $receiver }} *{{ $onebuilder }}) ExecX(ctx context.Context) {
{{- end }}
{{- range $e := $.Edges }}
{{- if and $e.Unique (not $e.Optional) }}
if _, ok := {{ $mutation }}.{{ $e.StructField }}ID(); {{ $mutation }}.{{ $e.StructField }}Cleared() && !ok {
if {{ $mutation }}.{{ $e.StructField }}Cleared() && len({{ $mutation }}.{{ $e.StructField }}IDs()) > 0 {
return errors.New(`{{ $pkg }}: clearing a required unique edge "{{ $.Name }}.{{ $e.Name }}"`)
}
{{- end }}