Commit 9e1f7bf
authored
Fix eager loading of hierarchies by defining the primary key
When preloading an association via join (e.g. by using `eager_load`, or by referencing the associated table in a `where` condition), rails uses the primary key of the associated table in order to deduplicate the rows and build up the correct association. But the default generated hierarchies table doesn't have an `id` column or anything else that rails can automatically detect as a primary key. This leads rails to incorrectly treat all the records as equal, and return only one at random.
For example, calling `MyModel.eager_load(:ancestor_hierarchies).map(&:ancestor_hierarchies)` currently returns incorrect data - only one hierarchy per model, no matter how deep the hierarchy actually is.
This PR tells rails to use the combination of the three columns (which do have a unique index on them already in the generator!) as the primary key, and allows the example code to correctly return all the hierarchies for each model.
It also would presumably fix #294 though I haven't tested that.1 parent b6f19d2 commit 9e1f7bf
1 file changed
+4
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
0 commit comments