We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
has_one association executes wrong update query when owner record is not composite keys and given :primary_key or :foreign_key option.
has_one
:primary_key
:foreign_key
class Employee < ActiveRecord::Base belongs_to :department, :foreign_key => [:department_id, :location_id] has_many :comments, :as => :person has_and_belongs_to_many :groups has_many :salaries, :primary_key => [:id, :location_id], :foreign_key => [:employee_id, :location_id] has_one :one_salary, :class_name => "Salary", :primary_key => [:id, :location_id], :foreign_key => [:employee_id, :location_id] end
def test_belongs_to_association_primary_key_and_foreign_key_are_present salary_01 = Salary.new(year: 2015, month: 1, employee_id: 5, location_id: 1) employee_01 = salary_01.create_employee salary_02 = Salary.new(year: 2015, month: 1, employee_id: 6, location_id: 1) employee_02 = salary_02.create_employee employee_01.reload employee_02.reload assert_equal(5, employee_01.id) assert_equal(1, employee_01.location_id) assert_equal(6, employee_02.id) assert_equal(1, employee_02.location_id) end
This test case is failed. salary_02.create_employee execute wrong update query as follows.
salary_02.create_employee
D, [2015-01-10T12:57:25.263568 #5446] DEBUG -- : (0.1ms) begin transaction D, [2015-01-10T12:57:25.265719 #5446] DEBUG -- : Employee Load (0.2ms) SELECT "employees".* FROM "employees" WHERE "employees"."id" = ? LIMIT 1 [["id", 1]] D, [2015-01-10T12:57:25.283642 #5446] DEBUG -- : (0.3ms) SAVEPOINT active_record_1 D, [2015-01-10T12:57:25.286847 #5446] DEBUG -- : SQL (0.4ms) INSERT INTO "salaries" ("year", "month", "employee_id", "location_id") VALUES (?, ?, ?, ?) [["year", 2015], ["month", 1], ["e mployee_id", 1], ["location_id", 1]] D, [2015-01-10T12:57:25.287443 #5446] DEBUG -- : (0.0ms) RELEASE SAVEPOINT active_record_1 D, [2015-01-10T12:57:25.289092 #5446] DEBUG -- : Salary Load (0.2ms) SELECT "salaries".* FROM "salaries" WHERE ("salaries"."employee_id" = 1 AND "salaries"."location_id" = 1) LIMIT 1 D, [2015-01-10T12:57:25.290366 #5446] DEBUG -- : Employee Load (0.1ms) SELECT "employees".* FROM "employees" WHERE "employees"."id" = ? LIMIT 1 [["id", 2]] D, [2015-01-10T12:57:25.291767 #5446] DEBUG -- : (0.1ms) SAVEPOINT active_record_1 D, [2015-01-10T12:57:25.292700 #5446] DEBUG -- : SQL (0.3ms) INSERT INTO "salaries" ("year", "month", "employee_id", "location_id") VALUES (?, ?, ?, ?) [["year", 2015], ["month", 1], ["e mployee_id", 2], ["location_id", 1]] D, [2015-01-10T12:57:25.293349 #5446] DEBUG -- : (0.1ms) RELEASE SAVEPOINT active_record_1 D, [2015-01-10T12:57:25.293884 #5446] DEBUG -- : Salary Load (0.1ms) SELECT "salaries".* FROM "salaries" WHERE ("salaries"."employee_id" = 1 AND "salaries"."location_id" = 1) LIMIT 1 D, [2015-01-10T12:57:25.294576 #5446] DEBUG -- : (0.1ms) SAVEPOINT active_record_1 D, [2015-01-10T12:57:25.295836 #5446] DEBUG -- : SQL (0.1ms) UPDATE "salaries" SET "employee_id" = ?, "location_id" = ? WHERE "salaries"."id" = 1 [["employee_id", nil], ["location_id", n il]] D, [2015-01-10T12:57:25.296260 #5446] DEBUG -- : (0.0ms) RELEASE SAVEPOINT active_record_1 D, [2015-01-10T12:57:25.297225 #5446] DEBUG -- : Salary Load (0.1ms) SELECT "salaries".* FROM "salaries" WHERE "salaries"."id" = ? LIMIT 1 [["id", 1]] D, [2015-01-10T12:57:25.297675 #5446] DEBUG -- : Salary Load (0.0ms) SELECT "salaries".* FROM "salaries" WHERE "salaries"."id" = ? LIMIT 1 [["id", 2]] D, [2015-01-10T12:57:25.298464 #5446] DEBUG -- : (0.3ms) rollback transaction F Finished in 0.164579s, 6.0761 runs/s, 6.0761 assertions/s. 1) Failure: TestAssociations#test_has_one_association_primary_key_and_foreign_key_are_present [test/test_associations.rb:93]: Expected: 1 Actual: nil
I think need add conditions in SingularAssociation#get_records_with_cpk_support.
diff --git a/lib/composite_primary_keys/associations/singular_association.rb b/lib/composite_primary_keys/associations/singular_association.rb index c8dbaaf..f14a3b5 100644 --- a/lib/composite_primary_keys/associations/singular_association.rb +++ b/lib/composite_primary_keys/associations/singular_association.rb @@ -3,7 +3,10 @@ module CompositePrimaryKeys extend ActiveSupport::Concern included do def get_records_with_cpk_support - cpk_applies = (target && target.composite?) || (owner && owner.composite?) + cpk_applies = (target && target.composite?) || + (owner && owner.composite?) || + (options[:primary_key] && options[:primary_key].kind_of?(Array)) || + (options[:foreign_key] && options[:foreign_key].kind_of?(Array)) return scope.limit(1).to_a if cpk_applies get_records_without_cpk_support end @@ -12,4 +15,4 @@ module CompositePrimaryKeys end end
What do you think?
The text was updated successfully, but these errors were encountered:
Hmm, I don't see any failing tests on the AR 4.2 branch (which is now master). Was this fixed, or am I missing something?
Sorry, something went wrong.
I tried merge this branch to master branch. But this problem was not solve yet.
Would you be so kind as to try master_with_pr270 in my repository(https://github.com/kirikak2/composite_primary_keys.git) and run test/test_association.rb.
master_with_pr270
Thanks.
Following up on this, can this be closed or is still an issue?
Sorry for my response. This probrem is solved. Thanks.
No branches or pull requests
has_one
association executes wrong update query when owner record is not composite keys and given:primary_key
or:foreign_key
option.This test case is failed.
salary_02.create_employee
execute wrong update query as follows.I think need add conditions in SingularAssociation#get_records_with_cpk_support.
What do you think?
The text was updated successfully, but these errors were encountered: