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 stale state for composite foreign keys in belongs_to association #50297
base: main
Are you sure you want to change the base?
Fix stale state for composite foreign keys in belongs_to association #50297
Conversation
@iamradioactive this PR introduces a new bug, which causes nested association to not be saved.. apparently it causes
Reproduction script: # frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
# gem "activerecord", "~> 7.1.0"
gem "rails", github: "iamradioactive/rails", branch: "handle_composite_foreign_keys"
gem "pg"
end
require "active_record"
require "logger"
require "minitest/autorun"
ActiveRecord::Base.establish_connection(
adapter: "postgresql",
database: "test",
encoding: "unicode",
host: "localhost",
port: "5432",
password: "12345",
username: "test")
ActiveRecord::Schema.define do
create_table :tenants, force: true do |t|
t.string :name
end
create_table :comments, force: true do |t|
t.integer :tenant_id
t.integer :post_id
t.integer :author_id
t.string :body
t.integer :position
end
create_table :posts, force: true do |t|
t.integer :tenant_id
end
create_table :author, force: true do |t|
t.integer :tenant_id
end
end
class Tenant < ActiveRecord::Base
end
class Post < ActiveRecord::Base
query_constraints :tenant_id, :id
has_many :comments
belongs_to :tenant
end
class Author < ActiveRecord::Base
query_constraints :tenant_id, :id
belongs_to :tenant
end
class Comment < ActiveRecord::Base
query_constraints :tenant_id, :id
before_save :touch_author
belongs_to :tenant
belongs_to :post
belongs_to :author
def touch_author
puts "author: #{self.author}"
end
end
class BugTest < Minitest::Test
def test_assoc
Author.destroy_all
Post.destroy_all
Comment.destroy_all
Tenant.destroy_all
tenant = Tenant.create!
post = Post.create!(tenant: tenant)
post.comments << Comment.new(body: "hello", tenant: tenant, author: Author.new(tenant: tenant))
post.save!
assert_equal 1, Post.count, "single post"
assert_equal 1, Comment.count, "2 comments"
assert_equal 1, Author.count, "1 author"
end
end This test passes on rails 7.1, but not on your branch. |
@yosiat , Interestingly, It seems to be another bug uncovered by these changes. Thank you for testing it out. I have updated the PR. On Rails 7.1 stable, if you verify the value of With Query Constraints
Without Query Constraints
Since, Stale state is not working correctly on main branch as of now, reload is not getting triggered when the tenant id changes and there is no loss of unsaved associations. After this update, |
If we are pivoting the fix from |
Thanks for your work and detailed investigation! I am bit confused, what this PR is going to fix? there were two issues:
I saw this comment about inversing (#50256 (comment)) which issue does it the address? the first one? |
These are two independent and valid bugs. I'm gonna open a separate PR to derive the foreign key using the commit and mark the new PR as a dependency for this one. There are two bugs to be fixed
I will update the PRs and drop a comment here. |
0192b35
to
6d89b87
Compare
I have updated the PR, this one contains only the changes related to the fix for cc: @nvasilevski @yosiat |
Can someone please rebase this branch as the branch suffers from #49597 and I think it makes noise in the verification/fix for this bug. |
6d89b87
to
eb922c9
Compare
@nvasilevski , Can you please review this again? The dependency #50347 is merged now |
Motivation / Background
Fixes #50256
Fixes #51231
Context :
Action :
model.association
to refer to the associated object.Current Behaviour:
Updated Behaviour:
Detail
This Pull Request updates
stale_state
method inbelongs_to association
to handle composite foreign keysAdditional information
For Additional context Please refer to #50256
DependencyThis PR has a dependency on Fix derived foreign key to return correctly when association id is part of query constraints #50347 and is to be merged after those changes are approved and merged.It is an independent bug, however when the stale state is updated correctly it results in loss of information because of the bug to be fixed in the mentioned PR.Please refer to this comment for additional description.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]