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 bug, when ':dependent => :destroy' violates foreign key constraints #12450
Conversation
@@ -752,4 +752,11 @@ | |||
|
|||
*Slava Markevich* | |||
|
|||
* Fixed a bug in `ActiveRecord::Associations::Builder` for case when `:dependent => :destroy' option |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move Changelog entry to the first line
Please use code style from: http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#write-your-code |
errors = [] | ||
begin | ||
player.destroy! | ||
rescue ActiveRecord::InvalidForeignKey => e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should try assert_nothing_raised
Updated pull request according code review marks. |
team = Team.create! | ||
player = Player.create! :id => 1, :team_id => team.id | ||
|
||
assert_nothing_raised { player.destroy! } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but I think better to remove assert_nothing_raised
(it is redudant) then your test will fail if destroy!
is broken
Please use |
@pftg So, I think it would be useful to have some style checker and run it before commiting |
|
||
def teardown | ||
ActiveRecord::Schema.define do | ||
drop_table :players, :if_exists => true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be great: drop_table :players, if_exists: true
Reindent methods and change hash-style |
The CHANGELOG on this could be clearer - this PR moves |
Update CHANGELOG.md |
👍 |
@rafaelfranca @tenderlove please review this request, because now, I have to do monkey patches on my production code for avoidance of this issue. |
@@ -831,3 +831,39 @@ def test_reflect_the_most_recent_change | |||
assert_equal post.author_id, author2.id | |||
end | |||
end | |||
|
|||
require 'models/team' | |||
require 'models/player' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a nitpick but could you please move the require statements to the very top of the file to follow the style of the file ? Nice fix though, thank you! ❤️
@robin850 |
@@ -0,0 +1,3 @@ | |||
class Player < ActiveRecord::Base | |||
belongs_to :team, :dependent => :destroy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be belongs_to :team, dependent: :destroy
👍 |
Updated test model for new Rails hash syntax |
@@ -831,3 +833,36 @@ def test_reflect_the_most_recent_change | |||
assert_equal post.author_id, author2.id | |||
end | |||
end | |||
|
|||
class BelongsToWithForeignKeyTest < ActiveRecord::TestCase | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@senny
Sorry, but I don`t understand what you want 😄 Do you offer to pick out this class to separate file ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We often use the ✂️ to mark blank lines that should be removed:
class BelongsToWithForeignKeyTest < ActiveRecord::TestCase
def setup
...
@senny |
look good to me. @rafaelfranca can you take a look? |
This look good but I'd like to avoid add new models to the test suire. @iantropov could you change the tests to reuse the models already there. If it is not possible let me know. Thanks |
@rafaelfranca please take a look. I've get rid of additional test models (Player and Team) and have reused existed (Author and AuthorAddress). |
what's the status of this? |
We are waiting @rafaelfranca |
any news? |
We are still waiting @rafaelfranca to accept this PR. |
@iantropov you would need to rebase your commit, it doesn't apply anymore |
👍 |
Fix bug, when ':dependent => :destroy' violates foreign key constraints Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/builder/association.rb activerecord/lib/active_record/associations/builder/has_one.rb
I don't feel confident enough to backport this commit to stable versions but it is on master. Since we have foreign keys support built in on rails now make sense to this behaviour just work. |
Fix bug, when ':dependent => :destroy' violates foreign key constraints Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/builder/association.rb activerecord/lib/active_record/associations/builder/has_one.rb Conflicts: activerecord/CHANGELOG.md
Fix issue, when ':dependent => :destroy' option violates foreign key constraints, issue #12380