Open namco1992 opened 3 years ago
Hi @namco1992. I think in the past we've had similar issues but the other way around. We've tried to reverse these loops but end up with similar behaviors. What really needs to be done is to remove the break
. But that causes all kinds of other problems. One day the hope is to deal with this via a rewrite of the eager loading bits.
In the meantime if you can find a solution that works for -all cases- I'd be glad to accept it.
Hello @aarondl, yes removing the break
will do too. May I ask what are the other problems
you mentioned and what kind of rewrite you want to do?
The main one is addressing the lack of inner joins where it would be possible to minimize round trips. It's one of the primary motivations to redo this susyem.
What version of SQLBoiler are you using (
sqlboiler --version
)?v4.2.0
What is your database and version (eg. Postgresql 10)
MySQL 5.7
Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)
(It's not a very realistic example but pls bear with it 😅 ) We have 3 entities here: person, bike and wheel. Person-bike and bike-wheel are M-M relationships. For example, Person A and Person B share the same bike, and the bike have 2 wheels. The relationships look like this:
The DB schema:
Further information. What did you do, what did you expect?
When we load the person's bikes and wheels like the code shows below:
We would expect that the bike should have 2 wheels since it's the same bike shared by 2 persons. However, you will get output like this:
As you see here, the second bike entity referred by person B doesn't have wheels, even though it's the same bike as person A owns.
The problem is in the M-M eager loading template. https://github.com/volatiletech/sqlboiler/blob/6ccc92cffb06c37bd14183ea1e103ff942aee22b/templates/09_relationship_to_many_eager.go.tpl#L152-L154
Firstly,
LoadBikes
actually loads 2 identical records from the DB, but they are different objects in our code. Then theLoadWheels
only load 2 wheels shared among the bikes. However, the outer loop exhausted the foreign results and assigned them to the first local object, therefore nothing left for the second one.It can be fixed by simply swapping the outer and inner loops. I've fixed it in our customized templates and it works fine. I can create a PR if you agree that it's something worth fixing. Thanks!