-
Notifications
You must be signed in to change notification settings - Fork 549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recursive eager loading with self association not populating references correctly #457
Comments
Quick question. Are there foreign keys to the |
Having a think on this it may be because there's a naive break somewhere in the association setting loop. Most of those loops look at foreign key of the object they have, look for an object with the same id, found it? Assign and stop looking. This could be complicated by the non-true join table though. Maybe some bad assumptions somewhere. |
hey @aarondl, thanks for taking a look. My tables are defined exactly as the snippet I posted above, following sqlboiler rule: The below is generated for
I use these "true join tables" all around my application, the exception being that this is the only one that references the same table on both keys. |
I actually totally missed the Some interesting developments here. I removed the
Notice the duplication of the 'a' entries. I think some deduplication optimization code has hosed us in a way here. It used to be if you did a query like this it would do something similar to this: select * from table where id in (1, 1, 1, 1) Instead what we do now is de-dup the 1 so that the query is more efficient and we only bring back as much data as we need. This however produces a single object (B in this case) (great! efficiency) that then gets set on all the C. So This is horrible in a number of ways. Unsure on a fix currently. Need to think more. |
These sort of references to references and one times better solution a cqrs layer on top. |
This is still a problem, and I'm still unsure of the best way to fix it. I think my only proposal would be pretty gross: Use context to be able to turn on a refcounter that will duplicate memory as needed to ensure that these references don't get changed on the same object. That way we keep the nice optimization for simple cases, but have a workaround for this terrible case. |
I think I get a similar behaviour in my case. I have two tables - action and action_version which have a one-to-many relationship. I start from a list of versions and want to fetch all sibling versions for each version in the list via qm.Load(), .e.g. []action_version -> []action -> [][]action_version. In the result, the versions I start from are listed twice for the version. Do you think this might be related? Some example code:
The result is as follows:
|
@e-nikolov It's likely to be yes. I wonder if you could post the same example with |
What you think of this approach? Sorry for the inline patch, I'm having trouble pushing to GitHub (ISP issues).
|
@vladvelici can you describe the change a bit? What about this approach solves the problem at hand but also keeps the optimization where possible? |
@aarondl to my understanding this is what the existing version does:
my proposal:
If I'm not wrong, this is about the same time complexity but it uses some extra memory for cross-referencing. Edit: I'm assuming the optimisation you're talking about is the |
Is this issue solved? I am facing the same issue where the break causes further elements of the slice to not have populated relations. |
If you're having a generation problem please answer these questions before submitting your issue. Thanks!
What version of SQLBoiler are you using (
sqlboiler --version
)?SQLBoiler v3.1.0
If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)
If this happened at runtime what code produced the issue? (if not applicable leave blank)
entities.Delimiters(qm.Load("Delimiters.Delimiters.Delimiters")).All(r.Context(), 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)Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)
PostgreSQL 10.4
Further information. What did you do, what did you expect?
When fetching D, D1, D2 and eager loading 3 levels ("Delimiters.Delimiters.Delimiters") one would expect for the references to be set for all Dx in all levels, instead, only the first row has all levels set:
D.R.Delimiters = [ C ]; C.R.Delimiters = [ B ]; B.R.Delimiters = [ A ]
D1.R.Delimiters = [ C ]; C.R.Delimiters = [ ];
D2.R.Delimiters = [ C ]; C.R.Delimiters = [ ];
Note: This problem occurs only for this specific association (self association/same table).
In cases where I have different associations, e.g.
Student.Course.Campus.School
, all associations are set for all records in all levels (instead of only the first row).Is this behaviour normal?
The text was updated successfully, but these errors were encountered: