Skip to content

Commit

Permalink
Fix bug with nullified foreign key back to root record
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Oct 31, 2024
1 parent 51168fc commit 893c3ca
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Bug with null foreign key to back to auxiliary `has_one` association with not matching names. E.g. user has many profiles and has one default profile, profile belongs to user.

## [0.6.0] - 2024-06-18

### Added
Expand Down
5 changes: 3 additions & 2 deletions lib/evil_seed/relation_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def setup_belongs_to_reflections
model_class.reflect_on_all_associations(:belongs_to).reject do |reflection|
next false if reflection.options[:polymorphic] # TODO: Add support for polymorphic belongs_to
included = root.included?("#{association_path}.#{reflection.name}")
excluded = root.excluded?("#{association_path}.#{reflection.name}") || reflection.name == inverse_reflection
excluded = root.excluded?("#{association_path}.#{reflection.name}")
inverse = reflection.name == inverse_reflection
if excluded and not included
if model_class.column_names.include?(reflection.foreign_key)
puts(" -- excluded #{reflection.foreign_key}") if verbose
Expand All @@ -182,7 +183,7 @@ def setup_belongs_to_reflections
foreign_keys[reflection.name] = reflection.foreign_key
table_names[reflection.name] = reflection.table_name
end
excluded and not included
excluded and not included or inverse
end
end

Expand Down
3 changes: 2 additions & 1 deletion test/db/models.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

class User < ActiveRecord::Base
has_one :profile
has_many :profiles
has_one :default_profile, -> { where(default: true) }, class_name: 'Profile'
belongs_to :forum

has_many :questions, foreign_key: :author_id
Expand Down
1 change: 1 addition & 0 deletions test/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def create_schema!
create_table :profiles do |t|
t.references :user, foreign_key: { on_delete: :cascade }
t.string :name
t.boolean :default, default: false
t.string :title
end

Expand Down
7 changes: 4 additions & 3 deletions test/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
Question.create!(question_attrs)

Profile.create!([
{ user: users[0], name: "Profile for user 0", title: "Title for user 0" },
{ user: users[1], name: "Profile for user 1", title: "Title for user 1" },
{ user: users[2], name: "Profile for user 2", title: "Title for user 2" },
{ user: users[0], default: true, name: "Default profile for user 0", title: "Default title for user 0" },
{ user: users[0], default: false, name: "Profile for user 0", title: "Title for user 0" },
{ user: users[1], default: true, name: "Profile for user 1", title: "Title for user 1" },
{ user: users[2], default: true, name: "Profile for user 2", title: "Title for user 2" },
])
3 changes: 3 additions & 0 deletions test/evil_seed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def setup
root.exclude('forum.users')
root.exclude(/parent\.users/)
root.exclude(/role\..+/)
root.exclude(/\.profiles/)
end
config.root('Question') do |root|
root.exclude(/.*/)
Expand Down Expand Up @@ -61,6 +62,8 @@ def test_it_dumps_and_restores
assert Role.find_by(name: 'Superadmin')
assert Question.find_by(name: 'fourth')
assert Question.find_by(name: 'fifth')
assert (Profile.count == 2) # Only default profiles for included users
assert_equal 1, User.find_by(login: 'johndoe').profiles.count
assert Profile.where.not(name: nil).none?
end
end
Expand Down

0 comments on commit 893c3ca

Please sign in to comment.