-
-
Notifications
You must be signed in to change notification settings - Fork 275
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
Fix exception when using a resource with a composite primary/foreign key association #3335
Conversation
Fix avo-hq#3334 Fix `NoMethodError: undefined method `to_sym' for an instance of Array` raised by this line when an association uses a composite primary key/foreign key.
Code Climate has analyzed commit 0a745ed and detected 0 issues on this pull request. View more on Code Climate. |
Thanks @filipegiusti! The approach I would take is to create a new model & resource which uses composite foreign keys and test that. Thank you for tackling this! |
I searched for a test file at PS: I also searched for some sort of coverage tool that would help pinpoint which tests cover that method and tried to get github copilot to find such tests, all without luck. |
Can you create a new spec next to this one and test only the composite behavior? |
@filipegiusti I can take over and create the test. Could you share some details about your environment setup? We don’t have any tables with composite primary or foreign keys, so I’m unsure how to configure the environment to test this change. |
It all started when we added the primary_key and foreign_key. We have these models. class Profile
has_one :membership, primary_key: %i[organization_id id], foreign_key: %i[organization_id profile_id], dependent: :destroy, inverse_of: :profile
end
class Membership
belongs_to :profile, primary_key: %i[organization_id id], foreign_key: %i[organization_id profile_id], inverse_of: :membership
end Both models have a primary key of id and an unique index of The edit page of Profile works, the exception is raised when Profile is attempted to be updated. We also have: class Resources::Profile < Avo::BaseResource
def fields
field :membership, as: :belongs_to, only_on: :show
end
end |
I might be able to look at this next week. |
This PR has been marked as stale because there was no activity for the past 15 days. |
That would be great, Thank you! |
This PR has been marked as stale because there was no activity for the past 15 days. |
Closing this because there was no activity for the past 15 days. Feel free to reopen if new information pops up ✌️ |
Hello! This was prioritized and I'll try to make this work in Avo. I see there are test models for Location and TeamMembership, could I modify and add a relationship between these? It would be something like: # in a migration
add_column :team_memberships, :location_id, :bigint
add_foreign_key :team_memberships, :locations, primary_key: %i[team_id id], column: %i[team_id location_id] # introduced on https://github.com/rails/rails/pull/47637
# in models
class Location
has_many :team_memberships, primary_key: %i[team_id id], foreign_key: %i[team_id location_id], inverse_of: :location
end
class TeamMembership
belongs_to :location, primary_key: %i[team_id id], foreign_key: %i[team_id location_id], inverse_of: :team_memberships
end This enforces a TeamMembership to only be linked to Location that shares the same Team. With that I should be able to create a test case that fails with the current code. |
If it doesn't affect the current tests, you can. However, I believe there are some tests related to those models and associations. The safest approach would be to establish a new association between existing models or create new ones specifically for this use case. |
I had a hard time creating the tests so I solved the problem in another way. We don't have a need to support composite foreign keys in associations anymore. |
Thanks for letting us know @filipegiusti I'm closing this PR to keep the queue clean, we might get back to it and reopen if necessary. |
Fixes #3334
Fix
NoMethodError: undefined method 'to_sym' for an instance of Array
raised by this line when an association uses a composite primary key/foreign key.Checklist: