Skip to content

Fix / Use table_name instead of relation name #1471

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jasonlgrd
Copy link

@Jasonlgrd Jasonlgrd commented Jun 18, 2025

This PR fix a bug. We have discuss it in #1450 PR.

Context

I have a user and patient_enrolled_journal_event resources

In user resource, I have this relation:

has_one :patient_enrolled_journal_event

In patient_enrolled_journal_event resource, I have this relation:

has_one :patient_enrolled_journal_event,
              exclude_links: :default,
              foreign_key_on: :related,
              relation_name: :patient_enrolled_journal_event

In PatientEnrolledJournalEvent model, I have this scope:

scope :active, lambda {
    where(archived_at: nil).left_outer_joins(:user).where.not(
      user: {
        id: nil
      }
    )
  }

And in PatientEnrolledJournalEventResource, I rewrite records method to apply this scope like this:

def self.records(options = {})
    super.active
  end

And when I get http://localhost:3001/api/v1/users?include=patient-enrolled-journal-event I have this SQL error :

SQLite3::SQLException: ambiguous column name: id:\nSELECT patient_enrolled_journal_events.*, \"id\" AS jr_source_id FROM \"patient_enrolled_journal_events\" INNER JOIN \"users\" \"user\" ON \"user\".\"id\" = \"patient_enrolled_journal_events\".\"user_id\" WHERE \"patient_enrolled_journal_events\".\"archived_at\" IS NULL AND \"user\".\"id\" IS NOT NULL AND \"patient_enrolled_journal_events\".\"id\" = ? ORDER BY patient_enrolled_journal_events.id asc\n^",

I have investigated, my breakpoint is in get_join_arel_node method:

last_join = join_sources.find { |j|
            valid_join_types.any? { |t| j.is_a?(t) } && j.left.name == table_name
          }

j.left.name = user
table_name = users
So, the condition is false :(

Problem

In the condition, we compare a relation name with table name and when the relation name is singular so isn't match with table_name.

Solution

The join_sources[0].left is a Arel::Nodes::TableAlias class so we have a table_name getter. It's better to compare the both table_name.

Checklists

All Submissions:

  • I've checked to ensure there aren't other open Pull Requests for the same update/change.
  • I've submitted a ticket for my issue if one did not already exist.
  • My submission passes all tests. (Please run the full test suite locally to cut down on noise from travis failures.)
  • I've used Github auto-closing keywords in the commit message or the description.
  • I've added/updated tests for this change.

New Feature Submissions:

  • I've submitted an issue that describes this feature, and received the go ahead from the maintainers.
  • My submission includes new tests.
  • My submission maintains compliance with JSON:API.

Bug fixes and Changes to Core Features:

  • I've included an explanation of what the changes do and why I'd like you to include them.
  • I've provided test(s) that fails without the change.

Test Plan:

Reviewer Checklist:

  • Maintains compliance with JSON:API
  • Adequate test coverage exists to prevent regressions

@Jasonlgrd Jasonlgrd changed the title fix: use table_name instead of relation name Fix / Use table_name instead of relation name Jun 18, 2025
@Jasonlgrd
Copy link
Author

ping @pjungwir

@Jasonlgrd Jasonlgrd marked this pull request as ready for review June 18, 2025 14:29
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.

1 participant