Giter Site home page Giter Site logo

Comments (6)

aarondl avatar aarondl commented on April 28, 2024 2

(Also fixed in dev) (edited)

from sqlboiler.

aarondl avatar aarondl commented on April 28, 2024

@jseriff This has a simple workaround. Setter problems on our end:

    parents, _ := models.Parents(db,
        qm.Load("Children.Thirds", "Children.Anothers"),
    ).All()

This code should work as expected (edited). Thanks for all these bugs btw!

from sqlboiler.

jseriff avatar jseriff commented on April 28, 2024

In testing this (in conjunction with #51), I ran into some strange behavior, which made this difficult to verify.

If you modify the example code above, and use only the following set of insert statements:

INSERT INTO test_schema.parents (name) values ('P1');
INSERT INTO test_schema.parents (name) values ('P2');
INSERT INTO test_schema.children (parent_id, name) values ((select id from test_schema.parents where name = 'P1'), 'C11');

the query now returns the following error:

ERROR: models: failed to assign all query results to Parent slice: failed to collect loaded Children: relationship struct was nil

in addition, I found that for the nested collections, the R struct is null if no children are found (similar to #51), but for the "children" level in the example.

I attempted to dump everything loaded by adding the following to the end of the sample program:

    for i := range parents {
        fmt.Printf("parents[%d].R: %v\n", i, parents[i].R)
        for j := range parents[i].R.Children {
            fmt.Printf("parents[%d].R.Children[%d].R: %v\n", i, j, parents[i].R.Children[j].R)
            for k := range parents[i].R.Children[j].R.Thirds {
                fmt.Printf("\t\tparents[%d].R.Children[%d].R.Thirds[%d]: %v\n", i, j, k, parents[i].R.Children[j].R.Thirds[k])
            }
            for k := range parents[i].R.Children[j].R.Anothers {
                fmt.Printf("\t\tparents[%d].R.Children[%d].R.Anothers[%d]: %v\n", i, j, k, parents[i].R.Children[j].R.Anothers[k])
            }
        }
    }

but it fails because parents[0].R.Children[0].R is nil. This is an example of a case where testing for a nil R could become quickly tedious.

from sqlboiler.

aarondl avatar aarondl commented on April 28, 2024

Hey @jseriff. I'm actually not seeing this on head of develop.

I used your schema above but removed all insert statements except for these:

INSERT INTO test_schema.parents (name) values ('P1');
INSERT INTO test_schema.parents (name) values ('P2');
INSERT INTO test_schema.children (parent_id, name) values ((select id from test_schema.parents where name = 'P1'), 'C11');
package main

import (
    "database/sql"
    "fmt"

    _ "github.com/lib/pq"
    "github.com/vattle/sqlboiler/boil"
    "github.com/vattle/sqlboiler/models"
    "github.com/vattle/sqlboiler/queries/qm"
)

func main() {
    db, _ := sql.Open("postgres", "")

    boil.DebugMode = true

    parents, err := models.Parents(db,
        //qm.Load("Children.Thirds"), // Tried both splitting and not splitting these
        qm.Load("Children.Thirds", "Children.Anothers"),
    ).All()
    if err != nil {
        fmt.Println(err)
    }

    for i := range parents {
        fmt.Printf("parents[%d].R: %v\n", i, parents[i].R)
        for j := range parents[i].R.Children {
            fmt.Printf("parents[%d].R.Children[%d].R: %v\n", i, j, parents[i].R.Children[j].R)
            for k := range parents[i].R.Children[j].R.Thirds {
                fmt.Printf("\t\tparents[%d].R.Children[%d].R.Thirds[%d]: %v\n", i, j, k, parents[i].R.Children[j].R.Thirds[k])
            }
            for k := range parents[i].R.Children[j].R.Anothers {
                fmt.Printf("\t\tparents[%d].R.Children[%d].R.Anothers[%d]: %v\n", i, j, k, parents[i].R.Children[j].R.Anothers[k])
            }
        }
    }
}
SELECT * FROM "test_schema"."parents";
[]
select * from "test_schema"."children" where "parent_id" in ($1,$2)
[1 2]
select * from "test_schema"."third" where "children_id" in ($1)
[1]
select * from "test_schema"."another" where "children_id" in ($1)
[1]
parents[0].R: &{[0xc4200151c0]}
parents[0].R.Children[0].R: &{<nil> [] []}
parents[1].R: &{[]}

And as for the R being nil, I thought I had fixed that too. As you can see, there is no "R" that is nil.

The R struct change actually requires regenerating your models. Are you sure you regenerated them?

from sqlboiler.

jseriff avatar jseriff commented on April 28, 2024

Sorry - I got lazy here, and didnt regenerate the models when verifying. This is fixed, thanks!.

from sqlboiler.

aarondl avatar aarondl commented on April 28, 2024

https://github.com/vattle/sqlboiler/releases :)

from sqlboiler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.