1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/schema
ktmouk 5b4a516c1c Fixed odd behavior of inverse_of with multiple belongs_to to same class
Fix: #35204.

This PR added validation to `automatic_inverse_of` that foreign_keys are the same.

If class has multiple `belongs_to` to same class, `automatic_inverse_of` can find the wrong `inverse_name`.

```ruby
class Room < ActiveRecord::Base
  belongs_to :user
  belongs_to :owner, class_name: "User"
end

class User < ActiveRecord::Base
  has_one :room
  has_one :owned_room, class_name: "Room", foreign_key: "owner_id"
end

user = User.create!
owned_room = Room.create!(owner: user)

p user.room
```

The current `automatic_inverse_of` validates the `reflection` that found from associations.
However, its validation does not validate that foreign keys are the same.

so this issue can be fixed by adding a validation of foreign keys.

Co-authored-by: alpaca-tc <alpaca-tc@alpaca.tc>
Co-authored-by: cat2koban <taba.noritomo@moneyforward.co.jp>
Co-authored-by: luccafort <konishi.tatsuro@moneyforward.co.jp>
2020-11-21 02:06:21 +09:00
..
mysql2_specific_schema.rb Use supports_datetime_with_precision? rather than subsecond_precision_supported? 2020-04-25 02:56:53 +09:00
oracle_specific_schema.rb Use squiggly heredoc to strip odd indentation in the executed SQL 2018-11-22 19:43:53 +09:00
postgresql_specific_schema.rb This PR adds support to retrieve partitioned indexes when asking for 2020-02-06 07:36:44 +02:00
schema.rb Fixed odd behavior of inverse_of with multiple belongs_to to same class 2020-11-21 02:06:21 +09:00
sqlite_specific_schema.rb add mysql and sqlite3 default test 2018-09-01 22:09:01 -04:00