Comments (6)
(Also fixed in dev) (edited)
from sqlboiler.
@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.
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.
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.
Sorry - I got lazy here, and didnt regenerate the models when verifying. This is fixed, thanks!.
from sqlboiler.
https://github.com/vattle/sqlboiler/releases :)
from sqlboiler.
Related Issues (20)
- AndIn generates a list of pairs instead of a flat list. HOT 1
- Non deterministic column binding when same column name across two tables (inner join) HOT 1
- Configuring aliases for relationships for sqlite is not working HOT 6
- types.Hstore does not works HOT 1
- Foreign key causes "panic: interface conversion: string is not error: missing method Error" HOT 1
- Error: can't evaluate field IsView in type drivers.Table HOT 2
- Bug/breaking change on upsert with postgres in v4.16.0 HOT 1
- OrWhere wrong in docs HOT 2
- `json_extract` not working with `Bind` HOT 1
- sqlboiler model generation not working with Vitess (MySQL CNFC scalable) due to subqueries HOT 1
- After specifying tag-ignore , sqlboiler is failing to generate the code HOT 3
- sqlboiler auto-generates replaced and unused (enum) type into boil_types.go HOT 8
- Timestamps in sqlite HOT 1
- Increase Depth Limit in ptrFromMapping Function for Deeper Structure Access in reflect.go
- SqlBoiler generates wrong table struct name HOT 2
- Compilation Errors with TIMESTAMP Columns in sqlite3 Driver
- Sqlite view model has wrong type and can't be replaced HOT 3
- Updating jsonb fields
- Switch to a different table but with the same schema while inserting the record
- Fix comment position of first column of table HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlboiler.