Skip to content

Conversation

@eapache-opslevel
Copy link

@eapache-opslevel eapache-opslevel commented Nov 17, 2025

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 from this gem 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.

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 ClosureTree#294 though I haven't tested that.
@seuros seuros requested a review from Copilot November 19, 2025 11:18
Copilot finished reviewing on behalf of seuros November 19, 2025 11:21
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in eager loading of hierarchy associations by defining a composite primary key for the dynamically generated hierarchy model classes. Without this fix, Rails treats all hierarchy records as identical when using eager_load or similar join-based preloading, causing incorrect data to be returned.

Key changes:

  • Adds a composite primary key definition to the dynamically created hierarchy classes, consisting of the three columns that already have a unique index

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unknown primary key for table page_hierarchies in model PageHierarchy

1 participant