diff --git a/doc/md/features.md b/doc/md/features.md index 4804de4caa..24ada915dd 100644 --- a/doc/md/features.md +++ b/doc/md/features.md @@ -80,6 +80,16 @@ The `namedges` option provides an API for preloading edges with custom names. This option can be added to a project using the `--feature namedges` flag, and you can learn more about in the [Eager Loading](eager-load.mdx) documentation. +### Bidirectional Edge Refs + +The `bidiedges` option guides Ent to set two-way references when eager-loading (O2M/O2O) edges. + +This option can be added to a project using the `--feature bidiedges` flag. + +:::note +Users that use the standard encoding/json.MarshalJSON should detach the circular references before calling `json.Marshal`. +::: + ### Schema Config The `sql/schemaconfig` option lets you pass alternate SQL database names to models. This is useful when your models don't all live under one database and are spread out across different schemas. diff --git a/entc/gen/feature.go b/entc/gen/feature.go index bb536ded37..ab2f5de350 100644 --- a/entc/gen/feature.go +++ b/entc/gen/feature.go @@ -51,6 +51,16 @@ var ( Description: "NamedEdges provides an API for eager-loading edges with dynamic names", } + // FeatureBidiEdgeRefs provides a feature-flag for sql dialect to set two-way + // references when loading (unique) edges. Note, users that use the standard + // encoding/json.MarshalJSON should detach the circular references before marshaling. + FeatureBidiEdgeRefs = Feature{ + Name: "bidiedges", + Stage: Experimental, + Default: false, + Description: "This features guides Ent to set two-way references when loading (O2M/O2O) edges", + } + // FeatureSnapshot stores a snapshot of ent/schema and auto-solve merge-conflict (issue #852). FeatureSnapshot = Feature{ Name: "schema/snapshot", @@ -140,6 +150,7 @@ var ( FeatureIntercept, FeatureEntQL, FeatureNamedEdges, + FeatureBidiEdgeRefs, FeatureSnapshot, FeatureSchemaConfig, FeatureLock, diff --git a/entc/gen/template/dialect/sql/query.tmpl b/entc/gen/template/dialect/sql/query.tmpl index 3132582148..192b2e3b14 100644 --- a/entc/gen/template/dialect/sql/query.tmpl +++ b/entc/gen/template/dialect/sql/query.tmpl @@ -80,7 +80,21 @@ func ({{ $receiver }} *{{ $builder }}) sqlAll(ctx context.Context, hooks ...quer if query := {{ $receiver }}.{{ $e.EagerLoadField }}; query != nil { if err := {{ $receiver }}.load{{ $e.StructField }}(ctx, query, nodes, {{ if $e.Unique }}nil{{ else }} func(n *{{ $.Name }}){ n.Edges.{{ $e.StructField }} = []*{{ $e.Type.Name }}{} }{{ end }}, - func(n *{{ $.Name }}, e *{{ $e.Type.Name }}){ n.Edges.{{ $e.StructField }} = {{ if $e.Unique }}e{{ else }}append(n.Edges.{{ $e.StructField }}, e){{ end }} }); err != nil { + {{- $lhs := printf "n.Edges.%s" $e.StructField }} + {{- $rhs := print "e" }}{{- if not $e.Unique }}{{ $rhs = printf "append(%s, e)" $lhs }}{{ end }} + {{- if and ($.FeatureEnabled "bidiedges") $e.Ref $e.Ref.Unique }} + func(n *{{ $.Name }}, e *{{ $e.Type.Name }}){ + {{ printf "%s = %s" $lhs $rhs }} + {{- $idx := $e.Ref.Index }} + {{- /* Set only in case this type was not loaded explicitly (without custom options). */}} + if !e.Edges.loadedTypes[{{ $idx }}] { + e.Edges.{{ $e.Ref.StructField }} = n + } + }); err != nil { + {{- else }} + {{- /* Keep it one-liner if there is not inverse-condition. */}} + func(n *{{ $.Name }}, e *{{ $e.Type.Name }}){ {{ printf "%s = %s" $lhs $rhs }} }); err != nil { + {{- end }} return nil, err } } diff --git a/entc/gen/template/ent.tmpl b/entc/gen/template/ent.tmpl index e4060e0ab8..32004d1463 100644 --- a/entc/gen/template/ent.tmpl +++ b/entc/gen/template/ent.tmpl @@ -52,8 +52,9 @@ type {{ $.Name }} struct { } {{- with $.Edges }} +{{- $edgesType := print $.Name "Edges"}} // {{ $.Name }}Edges holds the relations/edges for other nodes in the graph. -type {{ $.Name }}Edges struct { +type {{ $edgesType }} struct { {{- range $e := . }} {{- template "model/edgecomment" $e }} {{ $e.StructField }} {{ if not $e.Unique }}[]{{ end }}*{{ $e.Type.Name }} {{ with $e.StructTag }}`{{ . }}`{{ end }} @@ -68,16 +69,19 @@ type {{ $.Name }}Edges struct { {{- range $i, $e := . }} // {{ $e.StructField }}OrErr returns the {{ $e.StructField }} value or an error if the edge // was not loaded in eager-loading{{ if $e.Unique }}, or loaded but was not found{{ end }}. - func (e {{ $.Name }}Edges) {{ $e.StructField }}OrErr() ({{ if not $e.Unique }}[]{{ end }}*{{ $e.Type.Name }}, error) { - if e.loadedTypes[{{ $i }}] { - {{- if $e.Unique }} - if e.{{ $e.StructField }} == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: {{ $e.Type.Package }}.Label} - } - {{- end }} - return e.{{ $e.StructField }}, nil - } + func (e {{ $edgesType }}) {{ $e.StructField }}OrErr() ({{ if not $e.Unique }}[]{{ end }}*{{ $e.Type.Name }}, error) { + {{- if $e.Unique }} + if e.{{ $e.StructField }} != nil { + return e.{{ $e.StructField }}, nil + } else if e.loadedTypes[{{ $i }}] { + {{- /* Edge was loaded but was not found. */}} + return nil, &NotFoundError{label: {{ $e.Type.Package }}.Label} + } + {{- else }} + if e.loadedTypes[{{ $i }}] { + return e.{{ $e.StructField }}, nil + } + {{- end }} return nil, &NotLoadedError{edge: "{{ $e.Name }}"} } {{- end }} diff --git a/entc/gen/type.go b/entc/gen/type.go index ebb2f49cee..1b0628c4b4 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -2122,6 +2122,22 @@ func (e Edge) EntSQL() *entsql.Annotation { return sqlAnnotate(e.Annotations) } +// Index returns the index of the edge in the schema. +// Used mainly to extract its position in the "loadedTypes" array. +func (e Edge) Index() (int, error) { + // "owner" is the type that holds the edge. + owner := e.Owner + if e.IsInverse() { + owner = e.Ref.Type + } + for i, e1 := range owner.Edges { + if e1.Name == e.Name { + return i, nil + } + } + return 0, fmt.Errorf("edge %q was not found in its owner schema %q", e.Name, e.Owner.Name) +} + // Column returns the first element from the columns slice. func (r Relation) Column() string { if len(r.Columns) == 0 { diff --git a/entc/integration/cascadelete/ent/comment.go b/entc/integration/cascadelete/ent/comment.go index a67eb8523a..13300b1dc6 100644 --- a/entc/integration/cascadelete/ent/comment.go +++ b/entc/integration/cascadelete/ent/comment.go @@ -43,12 +43,10 @@ type CommentEdges struct { // PostOrErr returns the Post value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CommentEdges) PostOrErr() (*Post, error) { - if e.loadedTypes[0] { - if e.Post == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: post.Label} - } + if e.Post != nil { return e.Post, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: post.Label} } return nil, &NotLoadedError{edge: "post"} } diff --git a/entc/integration/cascadelete/ent/post.go b/entc/integration/cascadelete/ent/post.go index c09ca9f2f2..3fea6d45fa 100644 --- a/entc/integration/cascadelete/ent/post.go +++ b/entc/integration/cascadelete/ent/post.go @@ -45,12 +45,10 @@ type PostEdges struct { // AuthorOrErr returns the Author value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PostEdges) AuthorOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Author == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Author != nil { return e.Author, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "author"} } diff --git a/entc/integration/customid/ent/blob.go b/entc/integration/customid/ent/blob.go index f3baa57ec1..aa1b37e6e5 100644 --- a/entc/integration/customid/ent/blob.go +++ b/entc/integration/customid/ent/blob.go @@ -48,12 +48,10 @@ type BlobEdges struct { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e BlobEdges) ParentOrErr() (*Blob, error) { - if e.loadedTypes[0] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: blob.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: blob.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/customid/ent/bloblink.go b/entc/integration/customid/ent/bloblink.go index e38d8afcba..2193a07566 100644 --- a/entc/integration/customid/ent/bloblink.go +++ b/entc/integration/customid/ent/bloblink.go @@ -47,12 +47,10 @@ type BlobLinkEdges struct { // BlobOrErr returns the Blob value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e BlobLinkEdges) BlobOrErr() (*Blob, error) { - if e.loadedTypes[0] { - if e.Blob == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: blob.Label} - } + if e.Blob != nil { return e.Blob, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: blob.Label} } return nil, &NotLoadedError{edge: "blob"} } @@ -60,12 +58,10 @@ func (e BlobLinkEdges) BlobOrErr() (*Blob, error) { // LinkOrErr returns the Link value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e BlobLinkEdges) LinkOrErr() (*Blob, error) { - if e.loadedTypes[1] { - if e.Link == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: blob.Label} - } + if e.Link != nil { return e.Link, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: blob.Label} } return nil, &NotLoadedError{edge: "link"} } diff --git a/entc/integration/customid/ent/car.go b/entc/integration/customid/ent/car.go index 2298591f6c..67d0642196 100644 --- a/entc/integration/customid/ent/car.go +++ b/entc/integration/customid/ent/car.go @@ -46,12 +46,10 @@ type CarEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CarEdges) OwnerOrErr() (*Pet, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: pet.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: pet.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/customid/ent/device.go b/entc/integration/customid/ent/device.go index eed63278d7..764e8ef511 100644 --- a/entc/integration/customid/ent/device.go +++ b/entc/integration/customid/ent/device.go @@ -43,12 +43,10 @@ type DeviceEdges struct { // ActiveSessionOrErr returns the ActiveSession value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e DeviceEdges) ActiveSessionOrErr() (*Session, error) { - if e.loadedTypes[0] { - if e.ActiveSession == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: session.Label} - } + if e.ActiveSession != nil { return e.ActiveSession, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: session.Label} } return nil, &NotLoadedError{edge: "active_session"} } diff --git a/entc/integration/customid/ent/doc.go b/entc/integration/customid/ent/doc.go index dd21cb805a..67a427fcb4 100644 --- a/entc/integration/customid/ent/doc.go +++ b/entc/integration/customid/ent/doc.go @@ -46,12 +46,10 @@ type DocEdges struct { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e DocEdges) ParentOrErr() (*Doc, error) { - if e.loadedTypes[0] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: doc.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: doc.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/customid/ent/intsid.go b/entc/integration/customid/ent/intsid.go index 134d3609a3..65f915e3b9 100644 --- a/entc/integration/customid/ent/intsid.go +++ b/entc/integration/customid/ent/intsid.go @@ -42,12 +42,10 @@ type IntSIDEdges struct { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e IntSIDEdges) ParentOrErr() (*IntSID, error) { - if e.loadedTypes[0] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: intsid.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: intsid.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/customid/ent/note.go b/entc/integration/customid/ent/note.go index b6f8f6f0a1..4d01a29455 100644 --- a/entc/integration/customid/ent/note.go +++ b/entc/integration/customid/ent/note.go @@ -44,12 +44,10 @@ type NoteEdges struct { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NoteEdges) ParentOrErr() (*Note, error) { - if e.loadedTypes[0] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: note.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: note.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/customid/ent/pet.go b/entc/integration/customid/ent/pet.go index 1f271329e6..78672462f2 100644 --- a/entc/integration/customid/ent/pet.go +++ b/entc/integration/customid/ent/pet.go @@ -47,12 +47,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } @@ -78,12 +76,10 @@ func (e PetEdges) FriendsOrErr() ([]*Pet, error) { // BestFriendOrErr returns the BestFriend value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) BestFriendOrErr() (*Pet, error) { - if e.loadedTypes[3] { - if e.BestFriend == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: pet.Label} - } + if e.BestFriend != nil { return e.BestFriend, nil + } else if e.loadedTypes[3] { + return nil, &NotFoundError{label: pet.Label} } return nil, &NotLoadedError{edge: "best_friend"} } diff --git a/entc/integration/customid/ent/session.go b/entc/integration/customid/ent/session.go index 2223fa370a..a58e6e75f9 100644 --- a/entc/integration/customid/ent/session.go +++ b/entc/integration/customid/ent/session.go @@ -41,12 +41,10 @@ type SessionEdges struct { // DeviceOrErr returns the Device value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e SessionEdges) DeviceOrErr() (*Device, error) { - if e.loadedTypes[0] { - if e.Device == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: device.Label} - } + if e.Device != nil { return e.Device, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: device.Label} } return nil, &NotLoadedError{edge: "device"} } diff --git a/entc/integration/customid/ent/token.go b/entc/integration/customid/ent/token.go index 4224e9cf8c..7473fab778 100644 --- a/entc/integration/customid/ent/token.go +++ b/entc/integration/customid/ent/token.go @@ -43,12 +43,10 @@ type TokenEdges struct { // AccountOrErr returns the Account value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e TokenEdges) AccountOrErr() (*Account, error) { - if e.loadedTypes[0] { - if e.Account == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: account.Label} - } + if e.Account != nil { return e.Account, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: account.Label} } return nil, &NotLoadedError{edge: "account"} } diff --git a/entc/integration/customid/ent/user.go b/entc/integration/customid/ent/user.go index 5c391321e6..82b7f5bc84 100644 --- a/entc/integration/customid/ent/user.go +++ b/entc/integration/customid/ent/user.go @@ -54,12 +54,10 @@ func (e UserEdges) GroupsOrErr() ([]*Group, error) { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) ParentOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/edgefield/ent/card.go b/entc/integration/edgefield/ent/card.go index 28fd970bcb..a085a7982f 100644 --- a/entc/integration/edgefield/ent/card.go +++ b/entc/integration/edgefield/ent/card.go @@ -43,12 +43,10 @@ type CardEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CardEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/edgefield/ent/info.go b/entc/integration/edgefield/ent/info.go index c9055c844a..0703f8bb4a 100644 --- a/entc/integration/edgefield/ent/info.go +++ b/entc/integration/edgefield/ent/info.go @@ -42,12 +42,10 @@ type InfoEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e InfoEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } diff --git a/entc/integration/edgefield/ent/metadata.go b/entc/integration/edgefield/ent/metadata.go index b2bdad9c50..3739f20e1f 100644 --- a/entc/integration/edgefield/ent/metadata.go +++ b/entc/integration/edgefield/ent/metadata.go @@ -48,12 +48,10 @@ type MetadataEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e MetadataEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -70,12 +68,10 @@ func (e MetadataEdges) ChildrenOrErr() ([]*Metadata, error) { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e MetadataEdges) ParentOrErr() (*Metadata, error) { - if e.loadedTypes[2] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: metadata.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[2] { + return nil, &NotFoundError{label: metadata.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/edgefield/ent/node.go b/entc/integration/edgefield/ent/node.go index b4ea067825..7091ff32d5 100644 --- a/entc/integration/edgefield/ent/node.go +++ b/entc/integration/edgefield/ent/node.go @@ -44,12 +44,10 @@ type NodeEdges struct { // PrevOrErr returns the Prev value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) PrevOrErr() (*Node, error) { - if e.loadedTypes[0] { - if e.Prev == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Prev != nil { return e.Prev, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "prev"} } @@ -57,12 +55,10 @@ func (e NodeEdges) PrevOrErr() (*Node, error) { // NextOrErr returns the Next value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) NextOrErr() (*Node, error) { - if e.loadedTypes[1] { - if e.Next == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Next != nil { return e.Next, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "next"} } diff --git a/entc/integration/edgefield/ent/pet.go b/entc/integration/edgefield/ent/pet.go index 0b1d2dfd61..c1eb74806d 100644 --- a/entc/integration/edgefield/ent/pet.go +++ b/entc/integration/edgefield/ent/pet.go @@ -41,12 +41,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/edgefield/ent/post.go b/entc/integration/edgefield/ent/post.go index 14654b878c..8278f1e5e2 100644 --- a/entc/integration/edgefield/ent/post.go +++ b/entc/integration/edgefield/ent/post.go @@ -43,12 +43,10 @@ type PostEdges struct { // AuthorOrErr returns the Author value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PostEdges) AuthorOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Author == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Author != nil { return e.Author, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "author"} } diff --git a/entc/integration/edgefield/ent/rental.go b/entc/integration/edgefield/ent/rental.go index 582be3296a..b3952d5d8e 100644 --- a/entc/integration/edgefield/ent/rental.go +++ b/entc/integration/edgefield/ent/rental.go @@ -50,12 +50,10 @@ type RentalEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e RentalEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -63,12 +61,10 @@ func (e RentalEdges) UserOrErr() (*User, error) { // CarOrErr returns the Car value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e RentalEdges) CarOrErr() (*Car, error) { - if e.loadedTypes[1] { - if e.Car == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: car.Label} - } + if e.Car != nil { return e.Car, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: car.Label} } return nil, &NotLoadedError{edge: "car"} } diff --git a/entc/integration/edgefield/ent/user.go b/entc/integration/edgefield/ent/user.go index cd41ba6141..ab1f207bb9 100644 --- a/entc/integration/edgefield/ent/user.go +++ b/entc/integration/edgefield/ent/user.go @@ -71,12 +71,10 @@ func (e UserEdges) PetsOrErr() ([]*Pet, error) { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) ParentOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "parent"} } @@ -93,12 +91,10 @@ func (e UserEdges) ChildrenOrErr() ([]*User, error) { // SpouseOrErr returns the Spouse value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) SpouseOrErr() (*User, error) { - if e.loadedTypes[3] { - if e.Spouse == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Spouse != nil { return e.Spouse, nil + } else if e.loadedTypes[3] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "spouse"} } @@ -106,12 +102,10 @@ func (e UserEdges) SpouseOrErr() (*User, error) { // CardOrErr returns the Card value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) CardOrErr() (*Card, error) { - if e.loadedTypes[4] { - if e.Card == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: card.Label} - } + if e.Card != nil { return e.Card, nil + } else if e.loadedTypes[4] { + return nil, &NotFoundError{label: card.Label} } return nil, &NotLoadedError{edge: "card"} } @@ -119,12 +113,10 @@ func (e UserEdges) CardOrErr() (*Card, error) { // MetadataOrErr returns the Metadata value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) MetadataOrErr() (*Metadata, error) { - if e.loadedTypes[5] { - if e.Metadata == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: metadata.Label} - } + if e.Metadata != nil { return e.Metadata, nil + } else if e.loadedTypes[5] { + return nil, &NotFoundError{label: metadata.Label} } return nil, &NotLoadedError{edge: "metadata"} } diff --git a/entc/integration/edgeschema/ent/attachedfile.go b/entc/integration/edgeschema/ent/attachedfile.go index cbaa2dae21..ef540647c7 100644 --- a/entc/integration/edgeschema/ent/attachedfile.go +++ b/entc/integration/edgeschema/ent/attachedfile.go @@ -49,12 +49,10 @@ type AttachedFileEdges struct { // FiOrErr returns the Fi value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e AttachedFileEdges) FiOrErr() (*File, error) { - if e.loadedTypes[0] { - if e.Fi == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: file.Label} - } + if e.Fi != nil { return e.Fi, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: file.Label} } return nil, &NotLoadedError{edge: "fi"} } @@ -62,12 +60,10 @@ func (e AttachedFileEdges) FiOrErr() (*File, error) { // ProcOrErr returns the Proc value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e AttachedFileEdges) ProcOrErr() (*Process, error) { - if e.loadedTypes[1] { - if e.Proc == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: process.Label} - } + if e.Proc != nil { return e.Proc, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: process.Label} } return nil, &NotLoadedError{edge: "proc"} } diff --git a/entc/integration/edgeschema/ent/friendship.go b/entc/integration/edgeschema/ent/friendship.go index 7b1ac26767..e1d0bf4752 100644 --- a/entc/integration/edgeschema/ent/friendship.go +++ b/entc/integration/edgeschema/ent/friendship.go @@ -50,12 +50,10 @@ type FriendshipEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FriendshipEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -63,12 +61,10 @@ func (e FriendshipEdges) UserOrErr() (*User, error) { // FriendOrErr returns the Friend value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FriendshipEdges) FriendOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Friend == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Friend != nil { return e.Friend, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "friend"} } diff --git a/entc/integration/edgeschema/ent/grouptag.go b/entc/integration/edgeschema/ent/grouptag.go index 9987030852..40565673fd 100644 --- a/entc/integration/edgeschema/ent/grouptag.go +++ b/entc/integration/edgeschema/ent/grouptag.go @@ -46,12 +46,10 @@ type GroupTagEdges struct { // TagOrErr returns the Tag value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e GroupTagEdges) TagOrErr() (*Tag, error) { - if e.loadedTypes[0] { - if e.Tag == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: tag.Label} - } + if e.Tag != nil { return e.Tag, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: tag.Label} } return nil, &NotLoadedError{edge: "tag"} } @@ -59,12 +57,10 @@ func (e GroupTagEdges) TagOrErr() (*Tag, error) { // GroupOrErr returns the Group value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e GroupTagEdges) GroupOrErr() (*Group, error) { - if e.loadedTypes[1] { - if e.Group == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: group.Label} - } + if e.Group != nil { return e.Group, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: group.Label} } return nil, &NotLoadedError{edge: "group"} } diff --git a/entc/integration/edgeschema/ent/relationship.go b/entc/integration/edgeschema/ent/relationship.go index 1d83657eaa..dc303bb048 100644 --- a/entc/integration/edgeschema/ent/relationship.go +++ b/entc/integration/edgeschema/ent/relationship.go @@ -50,12 +50,10 @@ type RelationshipEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e RelationshipEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -63,12 +61,10 @@ func (e RelationshipEdges) UserOrErr() (*User, error) { // RelativeOrErr returns the Relative value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e RelationshipEdges) RelativeOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Relative == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Relative != nil { return e.Relative, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "relative"} } @@ -76,12 +72,10 @@ func (e RelationshipEdges) RelativeOrErr() (*User, error) { // InfoOrErr returns the Info value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e RelationshipEdges) InfoOrErr() (*RelationshipInfo, error) { - if e.loadedTypes[2] { - if e.Info == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: relationshipinfo.Label} - } + if e.Info != nil { return e.Info, nil + } else if e.loadedTypes[2] { + return nil, &NotFoundError{label: relationshipinfo.Label} } return nil, &NotLoadedError{edge: "info"} } diff --git a/entc/integration/edgeschema/ent/roleuser.go b/entc/integration/edgeschema/ent/roleuser.go index 0e55164faa..33d7fb7f1f 100644 --- a/entc/integration/edgeschema/ent/roleuser.go +++ b/entc/integration/edgeschema/ent/roleuser.go @@ -47,12 +47,10 @@ type RoleUserEdges struct { // RoleOrErr returns the Role value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e RoleUserEdges) RoleOrErr() (*Role, error) { - if e.loadedTypes[0] { - if e.Role == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: role.Label} - } + if e.Role != nil { return e.Role, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: role.Label} } return nil, &NotLoadedError{edge: "role"} } @@ -60,12 +58,10 @@ func (e RoleUserEdges) RoleOrErr() (*Role, error) { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e RoleUserEdges) UserOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } diff --git a/entc/integration/edgeschema/ent/tweetlike.go b/entc/integration/edgeschema/ent/tweetlike.go index 7aabc2c393..866e115dec 100644 --- a/entc/integration/edgeschema/ent/tweetlike.go +++ b/entc/integration/edgeschema/ent/tweetlike.go @@ -47,12 +47,10 @@ type TweetLikeEdges struct { // TweetOrErr returns the Tweet value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e TweetLikeEdges) TweetOrErr() (*Tweet, error) { - if e.loadedTypes[0] { - if e.Tweet == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: tweet.Label} - } + if e.Tweet != nil { return e.Tweet, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: tweet.Label} } return nil, &NotLoadedError{edge: "tweet"} } @@ -60,12 +58,10 @@ func (e TweetLikeEdges) TweetOrErr() (*Tweet, error) { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e TweetLikeEdges) UserOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } diff --git a/entc/integration/edgeschema/ent/tweettag.go b/entc/integration/edgeschema/ent/tweettag.go index b7901efee9..e0187ac21a 100644 --- a/entc/integration/edgeschema/ent/tweettag.go +++ b/entc/integration/edgeschema/ent/tweettag.go @@ -50,12 +50,10 @@ type TweetTagEdges struct { // TagOrErr returns the Tag value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e TweetTagEdges) TagOrErr() (*Tag, error) { - if e.loadedTypes[0] { - if e.Tag == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: tag.Label} - } + if e.Tag != nil { return e.Tag, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: tag.Label} } return nil, &NotLoadedError{edge: "tag"} } @@ -63,12 +61,10 @@ func (e TweetTagEdges) TagOrErr() (*Tag, error) { // TweetOrErr returns the Tweet value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e TweetTagEdges) TweetOrErr() (*Tweet, error) { - if e.loadedTypes[1] { - if e.Tweet == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: tweet.Label} - } + if e.Tweet != nil { return e.Tweet, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: tweet.Label} } return nil, &NotLoadedError{edge: "tweet"} } diff --git a/entc/integration/edgeschema/ent/usergroup.go b/entc/integration/edgeschema/ent/usergroup.go index 9685df7b63..90dfcb1335 100644 --- a/entc/integration/edgeschema/ent/usergroup.go +++ b/entc/integration/edgeschema/ent/usergroup.go @@ -49,12 +49,10 @@ type UserGroupEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserGroupEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -62,12 +60,10 @@ func (e UserGroupEdges) UserOrErr() (*User, error) { // GroupOrErr returns the Group value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserGroupEdges) GroupOrErr() (*Group, error) { - if e.loadedTypes[1] { - if e.Group == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: group.Label} - } + if e.Group != nil { return e.Group, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: group.Label} } return nil, &NotLoadedError{edge: "group"} } diff --git a/entc/integration/edgeschema/ent/usertweet.go b/entc/integration/edgeschema/ent/usertweet.go index 00079e337d..7ee168c218 100644 --- a/entc/integration/edgeschema/ent/usertweet.go +++ b/entc/integration/edgeschema/ent/usertweet.go @@ -49,12 +49,10 @@ type UserTweetEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserTweetEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -62,12 +60,10 @@ func (e UserTweetEdges) UserOrErr() (*User, error) { // TweetOrErr returns the Tweet value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserTweetEdges) TweetOrErr() (*Tweet, error) { - if e.loadedTypes[1] { - if e.Tweet == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: tweet.Label} - } + if e.Tweet != nil { return e.Tweet, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: tweet.Label} } return nil, &NotLoadedError{edge: "tweet"} } diff --git a/entc/integration/ent/card.go b/entc/integration/ent/card.go index 805b2fa264..a450f9bc44 100644 --- a/entc/integration/ent/card.go +++ b/entc/integration/ent/card.go @@ -57,12 +57,10 @@ type CardEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CardEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/ent/card_query.go b/entc/integration/ent/card_query.go index 6aae538341..cda811b0ce 100644 --- a/entc/integration/ent/card_query.go +++ b/entc/integration/ent/card_query.go @@ -449,7 +449,12 @@ func (cq *CardQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Card, e } if query := cq.withOwner; query != nil { if err := cq.loadOwner(ctx, query, nodes, nil, - func(n *Card, e *User) { n.Edges.Owner = e }); err != nil { + func(n *Card, e *User) { + n.Edges.Owner = e + if !e.Edges.loadedTypes[0] { + e.Edges.Card = n + } + }); err != nil { return nil, err } } diff --git a/entc/integration/ent/file.go b/entc/integration/ent/file.go index e019d77974..491d3fa89f 100644 --- a/entc/integration/ent/file.go +++ b/entc/integration/ent/file.go @@ -60,12 +60,10 @@ type FileEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FileEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } @@ -73,12 +71,10 @@ func (e FileEdges) OwnerOrErr() (*User, error) { // TypeOrErr returns the Type value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FileEdges) TypeOrErr() (*FileType, error) { - if e.loadedTypes[1] { - if e.Type == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: filetype.Label} - } + if e.Type != nil { return e.Type, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: filetype.Label} } return nil, &NotLoadedError{edge: "type"} } diff --git a/entc/integration/ent/filetype_query.go b/entc/integration/ent/filetype_query.go index e14208890d..bd8f6f2193 100644 --- a/entc/integration/ent/filetype_query.go +++ b/entc/integration/ent/filetype_query.go @@ -405,7 +405,12 @@ func (ftq *FileTypeQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Fi if query := ftq.withFiles; query != nil { if err := ftq.loadFiles(ctx, query, nodes, func(n *FileType) { n.Edges.Files = []*File{} }, - func(n *FileType, e *File) { n.Edges.Files = append(n.Edges.Files, e) }); err != nil { + func(n *FileType, e *File) { + n.Edges.Files = append(n.Edges.Files, e) + if !e.Edges.loadedTypes[1] { + e.Edges.Type = n + } + }); err != nil { return nil, err } } diff --git a/entc/integration/ent/generate.go b/entc/integration/ent/generate.go index ad9eb2eb9a..07a67f684f 100644 --- a/entc/integration/ent/generate.go +++ b/entc/integration/ent/generate.go @@ -4,4 +4,4 @@ package ent -//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature entql,sql/modifier,sql/lock,sql/upsert,sql/execquery,namedges --template ./template --header "// Copyright 2019-present Facebook Inc. All rights reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by ent, DO NOT EDIT." ./schema +//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature entql,sql/modifier,sql/lock,sql/upsert,sql/execquery,namedges,bidiedges --template ./template --header "// Copyright 2019-present Facebook Inc. All rights reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by ent, DO NOT EDIT." ./schema diff --git a/entc/integration/ent/group.go b/entc/integration/ent/group.go index b3dbe51fd7..a4e5cd2163 100644 --- a/entc/integration/ent/group.go +++ b/entc/integration/ent/group.go @@ -87,12 +87,10 @@ func (e GroupEdges) UsersOrErr() ([]*User, error) { // InfoOrErr returns the Info value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e GroupEdges) InfoOrErr() (*GroupInfo, error) { - if e.loadedTypes[3] { - if e.Info == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: groupinfo.Label} - } + if e.Info != nil { return e.Info, nil + } else if e.loadedTypes[3] { + return nil, &NotFoundError{label: groupinfo.Label} } return nil, &NotLoadedError{edge: "info"} } diff --git a/entc/integration/ent/groupinfo_query.go b/entc/integration/ent/groupinfo_query.go index 35523ea249..03f8d5ac2b 100644 --- a/entc/integration/ent/groupinfo_query.go +++ b/entc/integration/ent/groupinfo_query.go @@ -405,7 +405,12 @@ func (giq *GroupInfoQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*G if query := giq.withGroups; query != nil { if err := giq.loadGroups(ctx, query, nodes, func(n *GroupInfo) { n.Edges.Groups = []*Group{} }, - func(n *GroupInfo, e *Group) { n.Edges.Groups = append(n.Edges.Groups, e) }); err != nil { + func(n *GroupInfo, e *Group) { + n.Edges.Groups = append(n.Edges.Groups, e) + if !e.Edges.loadedTypes[3] { + e.Edges.Info = n + } + }); err != nil { return nil, err } } diff --git a/entc/integration/ent/node.go b/entc/integration/ent/node.go index 62cfd3b026..df4059e725 100644 --- a/entc/integration/ent/node.go +++ b/entc/integration/ent/node.go @@ -46,12 +46,10 @@ type NodeEdges struct { // PrevOrErr returns the Prev value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) PrevOrErr() (*Node, error) { - if e.loadedTypes[0] { - if e.Prev == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Prev != nil { return e.Prev, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "prev"} } @@ -59,12 +57,10 @@ func (e NodeEdges) PrevOrErr() (*Node, error) { // NextOrErr returns the Next value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) NextOrErr() (*Node, error) { - if e.loadedTypes[1] { - if e.Next == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Next != nil { return e.Next, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "next"} } diff --git a/entc/integration/ent/node_query.go b/entc/integration/ent/node_query.go index 08de90d315..03617d1fa3 100644 --- a/entc/integration/ent/node_query.go +++ b/entc/integration/ent/node_query.go @@ -446,13 +446,23 @@ func (nq *NodeQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Node, e } if query := nq.withPrev; query != nil { if err := nq.loadPrev(ctx, query, nodes, nil, - func(n *Node, e *Node) { n.Edges.Prev = e }); err != nil { + func(n *Node, e *Node) { + n.Edges.Prev = e + if !e.Edges.loadedTypes[1] { + e.Edges.Next = n + } + }); err != nil { return nil, err } } if query := nq.withNext; query != nil { if err := nq.loadNext(ctx, query, nodes, nil, - func(n *Node, e *Node) { n.Edges.Next = e }); err != nil { + func(n *Node, e *Node) { + n.Edges.Next = e + if !e.Edges.loadedTypes[0] { + e.Edges.Prev = n + } + }); err != nil { return nil, err } } diff --git a/entc/integration/ent/pet.go b/entc/integration/ent/pet.go index b7bb663fb7..b4087824a3 100644 --- a/entc/integration/ent/pet.go +++ b/entc/integration/ent/pet.go @@ -54,12 +54,10 @@ type PetEdges struct { // TeamOrErr returns the Team value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) TeamOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Team == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Team != nil { return e.Team, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "team"} } @@ -67,12 +65,10 @@ func (e PetEdges) TeamOrErr() (*User, error) { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/ent/pet_query.go b/entc/integration/ent/pet_query.go index fa340ef560..afa5aea730 100644 --- a/entc/integration/ent/pet_query.go +++ b/entc/integration/ent/pet_query.go @@ -446,7 +446,12 @@ func (pq *PetQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Pet, err } if query := pq.withTeam; query != nil { if err := pq.loadTeam(ctx, query, nodes, nil, - func(n *Pet, e *User) { n.Edges.Team = e }); err != nil { + func(n *Pet, e *User) { + n.Edges.Team = e + if !e.Edges.loadedTypes[7] { + e.Edges.Team = n + } + }); err != nil { return nil, err } } diff --git a/entc/integration/ent/user.go b/entc/integration/ent/user.go index 11e627d40b..96b27e77b0 100644 --- a/entc/integration/ent/user.go +++ b/entc/integration/ent/user.go @@ -94,12 +94,10 @@ type UserEdges struct { // CardOrErr returns the Card value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) CardOrErr() (*Card, error) { - if e.loadedTypes[0] { - if e.Card == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: card.Label} - } + if e.Card != nil { return e.Card, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: card.Label} } return nil, &NotLoadedError{edge: "card"} } @@ -161,12 +159,10 @@ func (e UserEdges) FollowingOrErr() ([]*User, error) { // TeamOrErr returns the Team value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) TeamOrErr() (*Pet, error) { - if e.loadedTypes[7] { - if e.Team == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: pet.Label} - } + if e.Team != nil { return e.Team, nil + } else if e.loadedTypes[7] { + return nil, &NotFoundError{label: pet.Label} } return nil, &NotLoadedError{edge: "team"} } @@ -174,12 +170,10 @@ func (e UserEdges) TeamOrErr() (*Pet, error) { // SpouseOrErr returns the Spouse value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) SpouseOrErr() (*User, error) { - if e.loadedTypes[8] { - if e.Spouse == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Spouse != nil { return e.Spouse, nil + } else if e.loadedTypes[8] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "spouse"} } @@ -196,12 +190,10 @@ func (e UserEdges) ChildrenOrErr() ([]*User, error) { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) ParentOrErr() (*User, error) { - if e.loadedTypes[10] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[10] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/ent/user_query.go b/entc/integration/ent/user_query.go index 459f153826..7dceb4642e 100644 --- a/entc/integration/ent/user_query.go +++ b/entc/integration/ent/user_query.go @@ -781,21 +781,36 @@ func (uq *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, e } if query := uq.withCard; query != nil { if err := uq.loadCard(ctx, query, nodes, nil, - func(n *User, e *Card) { n.Edges.Card = e }); err != nil { + func(n *User, e *Card) { + n.Edges.Card = e + if !e.Edges.loadedTypes[0] { + e.Edges.Owner = n + } + }); err != nil { return nil, err } } if query := uq.withPets; query != nil { if err := uq.loadPets(ctx, query, nodes, func(n *User) { n.Edges.Pets = []*Pet{} }, - func(n *User, e *Pet) { n.Edges.Pets = append(n.Edges.Pets, e) }); err != nil { + func(n *User, e *Pet) { + n.Edges.Pets = append(n.Edges.Pets, e) + if !e.Edges.loadedTypes[1] { + e.Edges.Owner = n + } + }); err != nil { return nil, err } } if query := uq.withFiles; query != nil { if err := uq.loadFiles(ctx, query, nodes, func(n *User) { n.Edges.Files = []*File{} }, - func(n *User, e *File) { n.Edges.Files = append(n.Edges.Files, e) }); err != nil { + func(n *User, e *File) { + n.Edges.Files = append(n.Edges.Files, e) + if !e.Edges.loadedTypes[0] { + e.Edges.Owner = n + } + }); err != nil { return nil, err } } @@ -829,7 +844,12 @@ func (uq *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, e } if query := uq.withTeam; query != nil { if err := uq.loadTeam(ctx, query, nodes, nil, - func(n *User, e *Pet) { n.Edges.Team = e }); err != nil { + func(n *User, e *Pet) { + n.Edges.Team = e + if !e.Edges.loadedTypes[0] { + e.Edges.Team = n + } + }); err != nil { return nil, err } } @@ -842,7 +862,12 @@ func (uq *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, e if query := uq.withChildren; query != nil { if err := uq.loadChildren(ctx, query, nodes, func(n *User) { n.Edges.Children = []*User{} }, - func(n *User, e *User) { n.Edges.Children = append(n.Edges.Children, e) }); err != nil { + func(n *User, e *User) { + n.Edges.Children = append(n.Edges.Children, e) + if !e.Edges.loadedTypes[10] { + e.Edges.Parent = n + } + }); err != nil { return nil, err } } diff --git a/entc/integration/gremlin/ent/card.go b/entc/integration/gremlin/ent/card.go index 8d3a3c788d..9b3cb7ac63 100644 --- a/entc/integration/gremlin/ent/card.go +++ b/entc/integration/gremlin/ent/card.go @@ -51,12 +51,10 @@ type CardEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CardEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/gremlin/ent/file.go b/entc/integration/gremlin/ent/file.go index accef61bee..9f19219cbc 100644 --- a/entc/integration/gremlin/ent/file.go +++ b/entc/integration/gremlin/ent/file.go @@ -53,12 +53,10 @@ type FileEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FileEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } @@ -66,12 +64,10 @@ func (e FileEdges) OwnerOrErr() (*User, error) { // TypeOrErr returns the Type value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FileEdges) TypeOrErr() (*FileType, error) { - if e.loadedTypes[1] { - if e.Type == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: filetype.Label} - } + if e.Type != nil { return e.Type, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: filetype.Label} } return nil, &NotLoadedError{edge: "type"} } diff --git a/entc/integration/gremlin/ent/group.go b/entc/integration/gremlin/ent/group.go index 8b69d3be3d..27ce8c9eb9 100644 --- a/entc/integration/gremlin/ent/group.go +++ b/entc/integration/gremlin/ent/group.go @@ -80,12 +80,10 @@ func (e GroupEdges) UsersOrErr() ([]*User, error) { // InfoOrErr returns the Info value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e GroupEdges) InfoOrErr() (*GroupInfo, error) { - if e.loadedTypes[3] { - if e.Info == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: groupinfo.Label} - } + if e.Info != nil { return e.Info, nil + } else if e.loadedTypes[3] { + return nil, &NotFoundError{label: groupinfo.Label} } return nil, &NotLoadedError{edge: "info"} } diff --git a/entc/integration/gremlin/ent/node.go b/entc/integration/gremlin/ent/node.go index d127cad4f5..f75ff37f8e 100644 --- a/entc/integration/gremlin/ent/node.go +++ b/entc/integration/gremlin/ent/node.go @@ -43,12 +43,10 @@ type NodeEdges struct { // PrevOrErr returns the Prev value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) PrevOrErr() (*Node, error) { - if e.loadedTypes[0] { - if e.Prev == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Prev != nil { return e.Prev, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "prev"} } @@ -56,12 +54,10 @@ func (e NodeEdges) PrevOrErr() (*Node, error) { // NextOrErr returns the Next value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) NextOrErr() (*Node, error) { - if e.loadedTypes[1] { - if e.Next == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Next != nil { return e.Next, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "next"} } diff --git a/entc/integration/gremlin/ent/pet.go b/entc/integration/gremlin/ent/pet.go index 9ce7091056..a6f32982f3 100644 --- a/entc/integration/gremlin/ent/pet.go +++ b/entc/integration/gremlin/ent/pet.go @@ -49,12 +49,10 @@ type PetEdges struct { // TeamOrErr returns the Team value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) TeamOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Team == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Team != nil { return e.Team, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "team"} } @@ -62,12 +60,10 @@ func (e PetEdges) TeamOrErr() (*User, error) { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/gremlin/ent/user.go b/entc/integration/gremlin/ent/user.go index e3c0339645..7a78d531f2 100644 --- a/entc/integration/gremlin/ent/user.go +++ b/entc/integration/gremlin/ent/user.go @@ -82,12 +82,10 @@ type UserEdges struct { // CardOrErr returns the Card value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) CardOrErr() (*Card, error) { - if e.loadedTypes[0] { - if e.Card == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: card.Label} - } + if e.Card != nil { return e.Card, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: card.Label} } return nil, &NotLoadedError{edge: "card"} } @@ -149,12 +147,10 @@ func (e UserEdges) FollowingOrErr() ([]*User, error) { // TeamOrErr returns the Team value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) TeamOrErr() (*Pet, error) { - if e.loadedTypes[7] { - if e.Team == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: pet.Label} - } + if e.Team != nil { return e.Team, nil + } else if e.loadedTypes[7] { + return nil, &NotFoundError{label: pet.Label} } return nil, &NotLoadedError{edge: "team"} } @@ -162,12 +158,10 @@ func (e UserEdges) TeamOrErr() (*Pet, error) { // SpouseOrErr returns the Spouse value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) SpouseOrErr() (*User, error) { - if e.loadedTypes[8] { - if e.Spouse == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Spouse != nil { return e.Spouse, nil + } else if e.loadedTypes[8] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "spouse"} } @@ -184,12 +178,10 @@ func (e UserEdges) ChildrenOrErr() ([]*User, error) { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) ParentOrErr() (*User, error) { - if e.loadedTypes[10] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[10] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/entc/integration/hooks/ent/card.go b/entc/integration/hooks/ent/card.go index 8c9c68741b..06eab60139 100644 --- a/entc/integration/hooks/ent/card.go +++ b/entc/integration/hooks/ent/card.go @@ -51,12 +51,10 @@ type CardEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CardEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/hooks/ent/pet.go b/entc/integration/hooks/ent/pet.go index d662a4b11c..397e4917c7 100644 --- a/entc/integration/hooks/ent/pet.go +++ b/entc/integration/hooks/ent/pet.go @@ -45,12 +45,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/hooks/ent/user.go b/entc/integration/hooks/ent/user.go index 6833fb3195..dfe13a47a3 100644 --- a/entc/integration/hooks/ent/user.go +++ b/entc/integration/hooks/ent/user.go @@ -82,12 +82,10 @@ func (e UserEdges) FriendsOrErr() ([]*User, error) { // BestFriendOrErr returns the BestFriend value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) BestFriendOrErr() (*User, error) { - if e.loadedTypes[3] { - if e.BestFriend == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.BestFriend != nil { return e.BestFriend, nil + } else if e.loadedTypes[3] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "best_friend"} } diff --git a/entc/integration/idtype/ent/user.go b/entc/integration/idtype/ent/user.go index d8dde5584d..23b1956fa0 100644 --- a/entc/integration/idtype/ent/user.go +++ b/entc/integration/idtype/ent/user.go @@ -45,12 +45,10 @@ type UserEdges struct { // SpouseOrErr returns the Spouse value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) SpouseOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Spouse == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Spouse != nil { return e.Spouse, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "spouse"} } diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index 34c2b26c3b..0e77bddee8 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -1853,6 +1853,12 @@ func EagerLoading(t *testing.T, client *ent.Client) { require.Len(a8m.Edges.Pets, 1) require.Equal("pedro", a8m.Edges.Pets[0].Name) require.Equal(nati.Name, a8m.Edges.Pets[0].Edges.Team.Name) + for _, p := range a8m.Edges.Pets { + require.Equal(a8m, p.Edges.Owner) + u, err := p.Edges.OwnerOrErr() + require.NoError(err) + require.Equal(a8m, u) + } a8m = client.User. Query(). diff --git a/entc/integration/migrate/entv1/car.go b/entc/integration/migrate/entv1/car.go index 3854576e0c..44cd8bbc6a 100644 --- a/entc/integration/migrate/entv1/car.go +++ b/entc/integration/migrate/entv1/car.go @@ -40,12 +40,10 @@ type CarEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CarEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/migrate/entv1/user.go b/entc/integration/migrate/entv1/user.go index ae9c805071..90abe88c21 100644 --- a/entc/integration/migrate/entv1/user.go +++ b/entc/integration/migrate/entv1/user.go @@ -71,12 +71,10 @@ type UserEdges struct { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) ParentOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "parent"} } @@ -93,12 +91,10 @@ func (e UserEdges) ChildrenOrErr() ([]*User, error) { // SpouseOrErr returns the Spouse value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) SpouseOrErr() (*User, error) { - if e.loadedTypes[2] { - if e.Spouse == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Spouse != nil { return e.Spouse, nil + } else if e.loadedTypes[2] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "spouse"} } @@ -106,12 +102,10 @@ func (e UserEdges) SpouseOrErr() (*User, error) { // CarOrErr returns the Car value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) CarOrErr() (*Car, error) { - if e.loadedTypes[3] { - if e.Car == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: car.Label} - } + if e.Car != nil { return e.Car, nil + } else if e.loadedTypes[3] { + return nil, &NotFoundError{label: car.Label} } return nil, &NotLoadedError{edge: "car"} } diff --git a/entc/integration/migrate/entv2/car.go b/entc/integration/migrate/entv2/car.go index 02d6a1636a..7036716eb9 100644 --- a/entc/integration/migrate/entv2/car.go +++ b/entc/integration/migrate/entv2/car.go @@ -42,12 +42,10 @@ type CarEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CarEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/migrate/entv2/pet.go b/entc/integration/migrate/entv2/pet.go index 1e32af3194..125ca5d5bc 100644 --- a/entc/integration/migrate/entv2/pet.go +++ b/entc/integration/migrate/entv2/pet.go @@ -42,12 +42,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/migrate/entv2/user.go b/entc/integration/migrate/entv2/user.go index 56b7844a96..455e651772 100644 --- a/entc/integration/migrate/entv2/user.go +++ b/entc/integration/migrate/entv2/user.go @@ -97,12 +97,10 @@ func (e UserEdges) CarOrErr() ([]*Car, error) { // PetsOrErr returns the Pets value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) PetsOrErr() (*Pet, error) { - if e.loadedTypes[1] { - if e.Pets == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: pet.Label} - } + if e.Pets != nil { return e.Pets, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: pet.Label} } return nil, &NotLoadedError{edge: "pets"} } diff --git a/entc/integration/multischema/ent/friendship.go b/entc/integration/multischema/ent/friendship.go index 731a1615e0..81165a2f8f 100644 --- a/entc/integration/multischema/ent/friendship.go +++ b/entc/integration/multischema/ent/friendship.go @@ -50,12 +50,10 @@ type FriendshipEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FriendshipEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -63,12 +61,10 @@ func (e FriendshipEdges) UserOrErr() (*User, error) { // FriendOrErr returns the Friend value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FriendshipEdges) FriendOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Friend == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Friend != nil { return e.Friend, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "friend"} } diff --git a/entc/integration/multischema/ent/pet.go b/entc/integration/multischema/ent/pet.go index 46ea609f43..5a4d291983 100644 --- a/entc/integration/multischema/ent/pet.go +++ b/entc/integration/multischema/ent/pet.go @@ -43,12 +43,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/multischema/versioned/friendship.go b/entc/integration/multischema/versioned/friendship.go index c5a3286443..b865b1db64 100644 --- a/entc/integration/multischema/versioned/friendship.go +++ b/entc/integration/multischema/versioned/friendship.go @@ -50,12 +50,10 @@ type FriendshipEdges struct { // UserOrErr returns the User value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FriendshipEdges) UserOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.User == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.User != nil { return e.User, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "user"} } @@ -63,12 +61,10 @@ func (e FriendshipEdges) UserOrErr() (*User, error) { // FriendOrErr returns the Friend value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FriendshipEdges) FriendOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Friend == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Friend != nil { return e.Friend, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "friend"} } diff --git a/entc/integration/multischema/versioned/pet.go b/entc/integration/multischema/versioned/pet.go index 2ad41fa50d..2d3819c7f8 100644 --- a/entc/integration/multischema/versioned/pet.go +++ b/entc/integration/multischema/versioned/pet.go @@ -43,12 +43,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/privacy/ent/task.go b/entc/integration/privacy/ent/task.go index c13ec95550..4dd8e75f2a 100644 --- a/entc/integration/privacy/ent/task.go +++ b/entc/integration/privacy/ent/task.go @@ -60,12 +60,10 @@ func (e TaskEdges) TeamsOrErr() ([]*Team, error) { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e TaskEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/entc/integration/template/ent/pet.go b/entc/integration/template/ent/pet.go index b179743b8a..4d902bdbba 100644 --- a/entc/integration/template/ent/pet.go +++ b/entc/integration/template/ent/pet.go @@ -44,12 +44,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/examples/edgeindex/ent/street.go b/examples/edgeindex/ent/street.go index 1353dccfcc..3e2176be79 100644 --- a/examples/edgeindex/ent/street.go +++ b/examples/edgeindex/ent/street.go @@ -42,12 +42,10 @@ type StreetEdges struct { // CityOrErr returns the City value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e StreetEdges) CityOrErr() (*City, error) { - if e.loadedTypes[0] { - if e.City == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: city.Label} - } + if e.City != nil { return e.City, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: city.Label} } return nil, &NotLoadedError{edge: "city"} } diff --git a/examples/fs/ent/file.go b/examples/fs/ent/file.go index 7be92cbadc..aa9f3570b0 100644 --- a/examples/fs/ent/file.go +++ b/examples/fs/ent/file.go @@ -46,12 +46,10 @@ type FileEdges struct { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e FileEdges) ParentOrErr() (*File, error) { - if e.loadedTypes[0] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: file.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: file.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/examples/jsonencode/ent/pet.go b/examples/jsonencode/ent/pet.go index cc5516a0ed..ab9694377a 100644 --- a/examples/jsonencode/ent/pet.go +++ b/examples/jsonencode/ent/pet.go @@ -46,12 +46,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/examples/migration/ent/card.go b/examples/migration/ent/card.go index d313fa2fb5..951290a9d9 100644 --- a/examples/migration/ent/card.go +++ b/examples/migration/ent/card.go @@ -50,12 +50,10 @@ type CardEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CardEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/examples/migration/ent/payment.go b/examples/migration/ent/payment.go index b58a3a7604..1b41710b14 100644 --- a/examples/migration/ent/payment.go +++ b/examples/migration/ent/payment.go @@ -52,12 +52,10 @@ type PaymentEdges struct { // CardOrErr returns the Card value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PaymentEdges) CardOrErr() (*Card, error) { - if e.loadedTypes[0] { - if e.Card == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: card.Label} - } + if e.Card != nil { return e.Card, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: card.Label} } return nil, &NotLoadedError{edge: "card"} } diff --git a/examples/migration/ent/pet.go b/examples/migration/ent/pet.go index 20772e8503..32d2fb20b5 100644 --- a/examples/migration/ent/pet.go +++ b/examples/migration/ent/pet.go @@ -52,12 +52,10 @@ type PetEdges struct { // BestFriendOrErr returns the BestFriend value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) BestFriendOrErr() (*Pet, error) { - if e.loadedTypes[0] { - if e.BestFriend == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: pet.Label} - } + if e.BestFriend != nil { return e.BestFriend, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: pet.Label} } return nil, &NotLoadedError{edge: "best_friend"} } @@ -65,12 +63,10 @@ func (e PetEdges) BestFriendOrErr() (*Pet, error) { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/examples/migration/ent/session.go b/examples/migration/ent/session.go index ab090e50e3..8cb37c5092 100644 --- a/examples/migration/ent/session.go +++ b/examples/migration/ent/session.go @@ -54,12 +54,10 @@ type SessionEdges struct { // DeviceOrErr returns the Device value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e SessionEdges) DeviceOrErr() (*SessionDevice, error) { - if e.loadedTypes[0] { - if e.Device == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: sessiondevice.Label} - } + if e.Device != nil { return e.Device, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: sessiondevice.Label} } return nil, &NotLoadedError{edge: "device"} } diff --git a/examples/o2m2types/ent/pet.go b/examples/o2m2types/ent/pet.go index b264948de4..1de8db92d8 100644 --- a/examples/o2m2types/ent/pet.go +++ b/examples/o2m2types/ent/pet.go @@ -42,12 +42,10 @@ type PetEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/examples/o2mrecur/ent/node.go b/examples/o2mrecur/ent/node.go index 51d21bf428..5e3473524b 100644 --- a/examples/o2mrecur/ent/node.go +++ b/examples/o2mrecur/ent/node.go @@ -44,12 +44,10 @@ type NodeEdges struct { // ParentOrErr returns the Parent value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) ParentOrErr() (*Node, error) { - if e.loadedTypes[0] { - if e.Parent == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Parent != nil { return e.Parent, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "parent"} } diff --git a/examples/o2o2types/ent/card.go b/examples/o2o2types/ent/card.go index ab7a2c8b7a..725212fe90 100644 --- a/examples/o2o2types/ent/card.go +++ b/examples/o2o2types/ent/card.go @@ -45,12 +45,10 @@ type CardEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CardEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/examples/o2o2types/ent/user.go b/examples/o2o2types/ent/user.go index 848276f0ca..72cf9ed16e 100644 --- a/examples/o2o2types/ent/user.go +++ b/examples/o2o2types/ent/user.go @@ -43,12 +43,10 @@ type UserEdges struct { // CardOrErr returns the Card value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) CardOrErr() (*Card, error) { - if e.loadedTypes[0] { - if e.Card == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: card.Label} - } + if e.Card != nil { return e.Card, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: card.Label} } return nil, &NotLoadedError{edge: "card"} } diff --git a/examples/o2obidi/ent/user.go b/examples/o2obidi/ent/user.go index a31c03f1e6..7ef45b358e 100644 --- a/examples/o2obidi/ent/user.go +++ b/examples/o2obidi/ent/user.go @@ -43,12 +43,10 @@ type UserEdges struct { // SpouseOrErr returns the Spouse value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) SpouseOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Spouse == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Spouse != nil { return e.Spouse, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "spouse"} } diff --git a/examples/o2orecur/ent/node.go b/examples/o2orecur/ent/node.go index a5dd5e3030..5f0dcc9e1f 100644 --- a/examples/o2orecur/ent/node.go +++ b/examples/o2orecur/ent/node.go @@ -44,12 +44,10 @@ type NodeEdges struct { // PrevOrErr returns the Prev value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) PrevOrErr() (*Node, error) { - if e.loadedTypes[0] { - if e.Prev == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Prev != nil { return e.Prev, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "prev"} } @@ -57,12 +55,10 @@ func (e NodeEdges) PrevOrErr() (*Node, error) { // NextOrErr returns the Next value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e NodeEdges) NextOrErr() (*Node, error) { - if e.loadedTypes[1] { - if e.Next == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: node.Label} - } + if e.Next != nil { return e.Next, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: node.Label} } return nil, &NotLoadedError{edge: "next"} } diff --git a/examples/privacytenant/ent/group.go b/examples/privacytenant/ent/group.go index cb559b0f96..73059ef7e9 100644 --- a/examples/privacytenant/ent/group.go +++ b/examples/privacytenant/ent/group.go @@ -45,12 +45,10 @@ type GroupEdges struct { // TenantOrErr returns the Tenant value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e GroupEdges) TenantOrErr() (*Tenant, error) { - if e.loadedTypes[0] { - if e.Tenant == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: tenant.Label} - } + if e.Tenant != nil { return e.Tenant, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: tenant.Label} } return nil, &NotLoadedError{edge: "tenant"} } diff --git a/examples/privacytenant/ent/user.go b/examples/privacytenant/ent/user.go index 54e9c30b67..9dbd1aa772 100644 --- a/examples/privacytenant/ent/user.go +++ b/examples/privacytenant/ent/user.go @@ -48,12 +48,10 @@ type UserEdges struct { // TenantOrErr returns the Tenant value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e UserEdges) TenantOrErr() (*Tenant, error) { - if e.loadedTypes[0] { - if e.Tenant == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: tenant.Label} - } + if e.Tenant != nil { return e.Tenant, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: tenant.Label} } return nil, &NotLoadedError{edge: "tenant"} } diff --git a/examples/start/ent/car.go b/examples/start/ent/car.go index a0f9cf5032..8c568f2583 100644 --- a/examples/start/ent/car.go +++ b/examples/start/ent/car.go @@ -45,12 +45,10 @@ type CarEdges struct { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e CarEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[0] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[0] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} } diff --git a/examples/traversal/ent/group.go b/examples/traversal/ent/group.go index 68fea93a4c..4d461ccbdb 100644 --- a/examples/traversal/ent/group.go +++ b/examples/traversal/ent/group.go @@ -53,12 +53,10 @@ func (e GroupEdges) UsersOrErr() ([]*User, error) { // AdminOrErr returns the Admin value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e GroupEdges) AdminOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Admin == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Admin != nil { return e.Admin, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "admin"} } diff --git a/examples/traversal/ent/pet.go b/examples/traversal/ent/pet.go index 7939c1172a..8785b95719 100644 --- a/examples/traversal/ent/pet.go +++ b/examples/traversal/ent/pet.go @@ -53,12 +53,10 @@ func (e PetEdges) FriendsOrErr() ([]*Pet, error) { // OwnerOrErr returns the Owner value or an error if the edge // was not loaded in eager-loading, or loaded but was not found. func (e PetEdges) OwnerOrErr() (*User, error) { - if e.loadedTypes[1] { - if e.Owner == nil { - // Edge was loaded but was not found. - return nil, &NotFoundError{label: user.Label} - } + if e.Owner != nil { return e.Owner, nil + } else if e.loadedTypes[1] { + return nil, &NotFoundError{label: user.Label} } return nil, &NotLoadedError{edge: "owner"} }