volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.74k stars 546 forks source link

Eager Loading Fails on NOT NULL columns? #953

Closed ItayGarin closed 3 years ago

ItayGarin commented 3 years ago

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

SQLBoiler v4.5.0

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

sqlite3

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

    txs, err := models.Transactions(qm.Load(models.TransactionRels.Coin)).
        All(context.Background(), db)

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)

SELECT * FROM "transactions";
[]
panic: reflect: call of reflect.Value.IsNil on int64 Value

goroutine 1 [running]:
reflect.Value.IsNil(...)
        /nix/store/qvxsh2nxjf598mzfi6hl9dy4qv3698x4-go-1.15.6/share/go/src/reflect/value.go:1086
github.com/volatiletech/sqlboiler/v4/queries.IsNil(0x72b5e0, 0x93d400, 0x1)
        /home/itayg/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.5.0/queries/reflect.go:745 +0x1da
crypto-log-boiler/models.transactionL.LoadFeeCoin(0x7d0a60, 0xc00001e0c0, 0x7d1620, 0xc000095790, 0x0, 0x720940, 0xc00000e7e0, 0x0, 0x0, 0x0, ...)
        /home/itayg/workspace/crypto-log/crypto-log-boiler/models/transactions.go:587 +0x1a5
reflect.Value.call(0xc000026700, 0xc000010e68, 0x13, 0x770822, 0x4, 0xc0000c8780, 0x6, 0xa, 0xa, 0x78, ...)
        /nix/store/qvxsh2nxjf598mzfi6hl9dy4qv3698x4-go-1.15.6/share/go/src/reflect/value.go:476 +0x8c7
reflect.Value.Call(0xc000026700, 0xc000010e68, 0x13, 0xc0000c8780, 0x6, 0xa, 0x5, 0xa, 0x0)
        /nix/store/qvxsh2nxjf598mzfi6hl9dy4qv3698x4-go-1.15.6/share/go/src/reflect/value.go:337 +0xb9
github.com/volatiletech/sqlboiler/v4/queries.loadRelationshipState.callLoadFunction(0x7d0a60, 0xc00001e0c0, 0x7fd3f973f7b8, 0xc000095790, 0xc0000fccf0, 0xc000012df0, 0x1, 0x1, 0x0, 0x0, ...)
        /home/itayg/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.5.0/queries/eager_load.go:195 +0x705
github.com/volatiletech/sqlboiler/v4/queries.loadRelationshipState.loadRelationships(0x7d0a60, 0xc00001e0c0, 0x7fd3f973f7b8, 0xc000095790, 0xc0000fccf0, 0xc000012df0, 0x1, 0x1, 0x0, 0x0, ...)
        /home/itayg/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.5.0/queries/eager_load.go:106 +0x72b
github.com/volatiletech/sqlboiler/v4/queries.eagerLoad(0x7d0a60, 0xc00001e0c0, 0x7fd3f973f7b8, 0xc000095790, 0xc000012d40, 0x1, 0x1, 0x0, 0x720940, 0xc00000e7e0, ...)
        /home/itayg/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.5.0/queries/eager_load.go:62 +0x1d5
github.com/volatiletech/sqlboiler/v4/queries.(*Query).Bind(0xc0000db500, 0x7d0a60, 0xc00001e0c0, 0x7fd3f973f7b8, 0xc000095790, 0x720940, 0xc00000e7e0, 0x2, 0x2)
        /home/itayg/go/pkg/mod/github.com/volatiletech/sqlboiler/v4@v4.5.0/queries/reflect.go:155 +0x385
crypto-log-boiler/models.transactionQuery.All(0xc0000db500, 0x7d0a60, 0xc00001e0c0, 0x7d1620, 0xc000095790, 0x3, 0xc00019bdc8, 0x1, 0x1, 0x0)
        /home/itayg/workspace/crypto-log/crypto-log-boiler/models/transactions.go:436 +0xa5
main.main()
        /home/itayg/workspace/crypto-log/crypto-log-boiler/main.go:49 +0x53e

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

simplified schema -

CREATE TABLE transactions (
    id INTEGER PRIMARY KEY,
    coin_id INTEGER NOT NULL,
    FOREIGN KEY(coin_id) REFERENCES coins(id)
);

CREATE TABLE coins (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL CHECK(name <> '')
);

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

Playing around with sqlboiler these past few days. Really liking it so far! Thanks for all the hard work!!

I believe the generated Load...() functions expect the column struct field to be nullable. Though, if I use the NOT NULL constraint in my schema, sqlboiler uses the regular int64 type for the field. If I remove the NOT NULL constraint, the code above works without crashing.

Am I'm missing something here? Would love to hear your thoughts :)

aarondl commented 3 years ago

So it seems to be maybe a failure in the templating where it doesn't understand that your column types are primitives? https://github.com/volatiletech/sqlboiler/blob/master/templates/07_relationship_to_one_eager.go.tpl#L29-L35

I'm not sure how this is happening. I generated code based on the schema you gave me but LoadFeeCoin doesn't exist so it's hard to verify. Can you please re-submit a more contained example?

aarondl commented 3 years ago

@ItayGarin it seems you attached an example and then deleted it. Will close this issue as I assume everything's been taken care of by this point.