Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kosyfrances opened this issue Mar 5, 2023 · 0 comments
Open

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

kosyfrances opened this issue Mar 5, 2023 · 0 comments
Labels

Comments

@kosyfrances
Copy link

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/v4@v4.14.1/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/v4@v4.14.1/queries/eager_load.go:195
    github.com/volatiletech/sqlboiler/v4/queries.loadRelationshipState.loadRelationships
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.14.1/queries/eager_load.go:106
    github.com/volatiletech/sqlboiler/v4/queries.eagerLoad
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.14.1/queries/eager_load.go:62
    github.com/volatiletech/sqlboiler/v4/queries.(*Query).Bind
      /Users/me/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.14.1/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants