Thanks to visit codestin.com
Credit goes to github.com

Skip to content

nil dereference when using through tables with composite key and custom index #2575

@Vilsol

Description

@Vilsol

Using the example schema below, on latest (unreleased) version using the features from #2560 when using a composite ID key and a custom index on a custom field there is a nil deferefence error.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

nil dereference panic is thrown

Expected Behavior 🤔

panic not to be thrown

Steps to Reproduce 🕹

Example schema. Tested on entgo.io/ent v0.10.2-0.20220528091257-0917701f9114

package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"time"
)

// User holds the schema definition for the User entity.
type User struct {
	ent.Schema
}

// Fields of the User.
func (User) Fields() []ent.Field {
	return []ent.Field{
		field.String("name").
			Default("Unknown"),
	}
}

// Edges of the User.
func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("friends", User.Type).
			Through("friendships", Friendship.Type),
	}
}

// Friendship holds the edge schema definition of the Friendship relationship.
type Friendship struct {
	ent.Schema
}

func (Friendship) Annotations() []schema.Annotation {
	return []schema.Annotation{
		field.ID("user_id", "friend_id"),
	}
}

// Fields of the Friendship.
func (Friendship) Fields() []ent.Field {
	return []ent.Field{
		field.Time("created_at").
			Default(time.Now),
		field.Int("user_id"),
		field.Int("friend_id"),
	}
}

// Edges of the Friendship.
func (Friendship) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("user", User.Type).
			Required().
			Unique().
			Field("user_id"),
		edge.To("friend", User.Type).
			Required().
			Unique().
			Field("friend_id"),
	}
}

func (Friendship) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("created_at"),
	}
}
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x8bbd3f]

goroutine 1 [running]:
entgo.io/ent/entc/gen.catch(0xc00098fab0)
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/entc/gen/graph.go:969 +0x94
panic({0x97f780, 0xe62910})
        /usr/lib/go/src/runtime/panic.go:838 +0x207
entgo.io/ent/entc/gen.(*Type).AddIndex(0xc000134380, 0xc000a30960)
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/entc/gen/type.go:622 +0x2ff
entgo.io/ent/entc/gen.(*Graph).addIndexes(0xc000921340?, 0xc00012f2c0)
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/entc/gen/graph.go:267 +0x125
entgo.io/ent/entc/gen.NewGraph(0xc00012e3c0, {0xc000a76520, 0x2, 0x2})
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/entc/gen/graph.go:160 +0x3e5
entgo.io/ent/entc.LoadGraph({0x7ffec934b309, 0xb}, 0xc00012e3c0)
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/entc/entc.go:40 +0xef
entgo.io/ent/entc.generate({0x7ffec934b309, 0xb}, 0x50?)
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/entc/entc.go:363 +0x2c
entgo.io/ent/entc.Generate({0x7ffec934b309, 0xb}, 0xc00012e3c0, {0xc000319d40, 0x2, 0x979500?})
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/entc/entc.go:86 +0x21f
entgo.io/ent/cmd/internal/base.GenerateCmd.func1(0xc00014f180?, {0xc00007ed70, 0x1, 0x5?})
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/cmd/internal/base/base.go:160 +0x2af
github.com/spf13/cobra.(*Command).execute(0xc00014f180, {0xc00007ed20, 0x5, 0x5})
        /home/vilsol/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:860 +0x663
github.com/spf13/cobra.(*Command).ExecuteC(0xc00014ea00)
        /home/vilsol/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:974 +0x3b4
github.com/spf13/cobra.(*Command).Execute(...)
        /home/vilsol/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:902
main.main()
        /home/vilsol/go/pkg/mod/entgo.io/ent@v0.10.2-0.20220528091257-0917701f9114/cmd/ent/ent.go:23 +0xb0

Your Environment 🌎

Tech Version
Go 1.18.2
Ent v0.10.2-0.20220528091257-0917701f9114
Database PSQL
Driver github.com/jackc/pgx/v4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions