volatiletech / sqlboiler

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

Type assertion error at multi-level eager loading #124

Closed tnii closed 7 years ago

tnii commented 7 years ago

Hi, I am new to GitHub.

When I trying multi-level eager load in different child tables through common parent, runtime error occurred.

Table schema and rows:

CREATE TABLE parents (
  id SERIAL PRIMARY KEY
);
CREATE TABLE a_children (
  id SERIAL PRIMARY KEY,
  parent_id BIGINT UNSIGNED DEFAULT NULL,
  CONSTRAINT a_children_parents FOREIGN KEY(parent_id) REFERENCES parents(id)
);
CREATE TABLE b_children (
  id SERIAL PRIMARY KEY,
  parent_id BIGINT UNSIGNED DEFAULT NULL,
  CONSTRAINT b_children_parents FOREIGN KEY(parent_id) REFERENCES parents(id)
);

INSERT INTO parents (id) VALUES (1,1);
INSERT INTO a_children (id,parent_id) VALUES (1,1);
INSERT INTO b_children (id,parent_id) VALUES (1,1);

Code for reproduction:

package main

import (
    "_play/models"
    "database/sql"

    _ "github.com/go-sql-driver/mysql"
    "github.com/vattle/sqlboiler/queries/qm"
)

func main() {
    db, _ := sql.Open("mysql", "root:super-secret-password@tcp(localhost:3306)/test?charset=utf8&parseTime=true")

    // (1) success
    models.AChildren(db, qm.Load("Parent")).One()

    // (2) success
    models.AChildren(db, qm.Load("Parent.BChildren"), qm.Where("id=?", 1)).One()

    // (3) fail
    models.AChildren(db, qm.Load("Parent.BChildren")).All()
}

Execute line of (3) above, type assertion error is shown:

panic: interface conversion: interface {} is *[]*models.Parent, not *models.ParentSlice

goroutine 1 [running]:
_play/models.parentL.LoadBChildren(0x1457640, 0xc42009cc80, 0x0, 0x12ad1e0, 0xc42008d8a0, 0x0, 0x0)
    _play/models/parents.go:453 +0xaf2
reflect.Value.call(0xc420099200, 0xc42008ee80, 0x13, 0x13156a0, 0x4, 0xc4200c5a70, 0x4, 0x4, 0xc4201163d8, 0x12ba720, ...)
    /usr/local/Cellar/go/1.8/libexec/src/reflect/value.go:434 +0x91f
reflect.Value.Call(0xc420099200, 0xc42008ee80, 0x13, 0xc4200c5a70, 0x4, 0x4, 0xc42008d7f0, 0x199, 0xc420099200)
    /usr/local/Cellar/go/1.8/libexec/src/reflect/value.go:302 +0xa4
github.com/vattle/sqlboiler/queries.loadRelationshipState.callLoadFunction(0x1457640, 0xc42009cc80, 0xc4200e7410, 0xc42008d780, 0x2, 0x2, 0x1, 0x12ad1e0, 0xc42008d8a0, 0x16, ...)
    github.com/vattle/sqlboiler/queries/eager_load.go:184 +0x40d
github.com/vattle/sqlboiler/queries.loadRelationshipState.loadRelationships(0x1457640, 0xc42009cc80, 0xc4200e7410, 0xc42008d780, 0x2, 0x2, 0x1, 0x12ad1e0, 0xc42008d8a0, 0x2, ...)
    github.com/vattle/sqlboiler/queries/eager_load.go:101 +0x598
github.com/vattle/sqlboiler/queries.loadRelationshipState.loadRelationships(0x1457640, 0xc42009cc80, 0xc4200e7410, 0xc42008d780, 0x2, 0x2, 0x0, 0x12fd180, 0xc42008d640, 0x2, ...)
    github.com/vattle/sqlboiler/queries/eager_load.go:139 +0x410
github.com/vattle/sqlboiler/queries.eagerLoad(0x1457640, 0xc42009cc80, 0xc4201161c0, 0x1, 0x1, 0x12fd180, 0xc42008d640, 0x2, 0x0, 0x0)
    github.com/vattle/sqlboiler/queries/eager_load.go:57 +0x166
github.com/vattle/sqlboiler/queries.(*Query).Bind(0xc420118000, 0x12fd180, 0xc42008d640, 0x0, 0x0)
    github.com/vattle/sqlboiler/queries/reflect.go:114 +0x1cb
_play/models.aChildQuery.All(0xc420118000, 0xc42009cc80, 0xc4201161b0, 0x2, 0x2, 0xc420118000)
    _play/models/a_children.go:258 +0x71
main.main()
    _play/main.go:21 +0x33b
exit status 2

It seems that []models.Parent come from reflect.SliceOf() at collectLoaded().

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

SQLBoiler v2.2.0

aarondl commented 7 years ago

Resolved on the dev branch. Thanks for bringing it up :)

tnii commented 7 years ago

This is working fine. Thank you!