This project is archived and is in readonly mode.

#5451 ✓stale
Matt Scilipoti

belongs_to relationship creation should fail if foreign key does not exist.

Reported by Matt Scilipoti | August 25th, 2010 @ 02:40 AM

ItemA belongs_to ItemB
item_b_id is not in table ItemAs.

ItemA.new.create_item_b -> does NOT raise error

A new ItemB is saved to db.

ItemA.first.item_b == nil

Env:
ree 1.8.7 2010.02
rails 3.0.0.rc, 3.0.0.rc2

This test identifies the issue. It can be placed in AR test/cases/associations/belongs_to_associations_test.rb


  class ItemA < ActiveRecord::Base
    belongs_to :item_b
  end
  class ItemB < ActiveRecord::Base
  end

  def test_creating_the_belonging_object_when_foreign_key_does_not_exist
    ActiveRecord::Base.connection.create_table :item_as, :force => true do |t|
      t.string :name
    end

    ActiveRecord::Base.connection.create_table :item_bs, :force => true do |t|
      t.string :name
    end
    
    item_a = ItemA.create!("name" => "Apple")
    item_b = item_a.create_item_b("name" => "(fan)Boy")
    assert_equal item_b, item_a.item_b
    item_a.save!
    item_a = ItemA.last
    assert_equal item_b, item_a.item_b
  end

Comments and changes to this ticket

  • Matt Scilipoti

    Matt Scilipoti August 25th, 2010 @ 02:15 PM

    This test identifies the issue but, it may be better to use:

    item_a = ItemA.create!("name" => "Apple")
    assert_raise <insert appropriate error here> do
      item_b = item_a.create_item_b("name" => "(fan)Boy")
    end
    
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:33 PM

    • State changed from “new” to “open”
    • Tag changed from rails 3.0.rc, rails 3.rc2 to rails 30rc, rails 3rc2

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:33 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages