Skip to content

Panic when non nullable field references a nullable foreign keyΒ #1252

@kosyfrances

Description

@kosyfrances

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

v4.14.1

What is your database and version (eg. Postgresql 10)

Postgresql 13.1

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

CREATE TABLE IF NOT EXISTS animals (
    id NOT NULL PRIMARY KEY,
    specie VARCHAR(200) UNIQUE
)

CREATE TABLE IF NOT EXISTS cats (
    id NOT NULL PRIMARY KEY,
    specie VARCHAR(200) NOT NULL UNIQUE REFERENCES animals(specie)
)

Further information. What did you do, what did you expect?

I had a function like this to get cat info by specie, loading the Animals relation alongside:

func GetCatInfo(ctx context.Context, specie string) (*models.Cats, error) {
	return models.Cats(
		models.CatsWhere.Specie.EQ(specie),
		qm.Load(models.CatsRels.SpecieAnimals),
	).One(ctx, db)
}

When called, it panicked instead:

 panic: reflect: call of reflect.Value.IsNil on string Value
 
 -> reflect.Value.IsNil
 ->   /usr/local/Cellar/go/1.19.4/libexec/src/reflect/value.go:1554

    github.com/volatiletech/sqlboiler/v4/queries.IsNil
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/[email protected]/queries/reflect.go:769
    github.com/vdb/models.catsL.LoadSpecieAnimals
      /Users/me/vdb/models/cats.go:507
    reflect.Value.call
      /usr/local/Cellar/go/1.19.4/libexec/src/reflect/value.go:584
    reflect.Value.Call
      /usr/local/Cellar/go/1.19.4/libexec/src/reflect/value.go:368
    github.com/volatiletech/sqlboiler/v4/queries.loadRelationshipState.callLoadFunction
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/[email protected]/queries/eager_load.go:195
    github.com/volatiletech/sqlboiler/v4/queries.loadRelationshipState.loadRelationships
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/[email protected]/queries/eager_load.go:106
    github.com/volatiletech/sqlboiler/v4/queries.eagerLoad
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/[email protected]/queries/eager_load.go:62
    github.com/volatiletech/sqlboiler/v4/queries.(*Query).Bind
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/[email protected]/queries/reflect.go:158
    github.com/vdb/models.catsQuery.One
      /Users/me/vdb/models/cats.go:397

Basically, because specie on cats table is a string, it breaks at this part of the code:

		if !queries.IsNil(object.Specie) {
			args = append(args, object.Specie)
		}

When I removed the not null constraint on the cats table, the error was gone, because specie is now null.String

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions