# Problem: Foreign Key in INSERT is NULL

**URL:** <https://www.ruby-forum.com/t/problem-foreign-key-in-insert-is-null/106818>\
**Category:** Rails\
**Created:** [June 14, 2007, 11:32am UTC](https://www.ruby-forum.com/t/problem-foreign-key-in-insert-is-null/106818 "2007-06-14T11:32:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyrubyruby\_onrail](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@rubyrubyruby\_onrail](https://www.ruby-forum.com/u/rubyrubyruby_onrail)\
**Post date:** [June 14, 2007, 11:32am UTC](https://www.ruby-forum.com/t/problem-foreign-key-in-insert-is-null/106818/1 "2007-06-14T11:32:11Z")

</div>

Using a collection\_select in the view, I have added the foreign key to  
the parameter list - good so far. The log file confirms this:

# The model is an item (in the Items database table), the supplier\_id is

the foreign key in the same table.

Parameters: {“commit”=\>“Create”, “action”=\>“create”,  
“controller”=\>“item”, “record”=\>{“name”=\>“My Object”,  
“description”=\>“Large object”, “supplier\_id”=\>“10001”}}

However, the next line in the log file says:

Cannot insert the value NULL into column ‘supplier\_id’, table  
‘RubyOnRailsTestingDB.dbo.items’; column does not allow nulls. INSERT  
fails.

Can anyone explain why the supplier\_id would be null in the INSERT when  
the parameters list clearly shows that the supplier\_id is “10001”? (As  
you can see, the other parameters are inserted properly.)

Thanks in advance,

C

---

<div class="post-metadata">

**Author:** ![rubyrubyruby\_onrail](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@rubyrubyruby\_onrail](https://www.ruby-forum.com/u/rubyrubyruby_onrail)\
**Post date:** [June 14, 2007, 11:38am UTC](https://www.ruby-forum.com/t/problem-foreign-key-in-insert-is-null/106818/2 "2007-06-14T11:38:09Z")

</div>

weird…

just a shot in the (very?) dark … did you define any association in  
the model (models/Item.rb)?

---

<div class="post-metadata">

**Author:** ![rubyrubyruby\_onrail](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@rubyrubyruby\_onrail](https://www.ruby-forum.com/u/rubyrubyruby_onrail)\
**Post date:** [June 14, 2007, 11:40am UTC](https://www.ruby-forum.com/t/problem-foreign-key-in-insert-is-null/106818/3 "2007-06-14T11:40:50Z")

</div>

My Item.rb is below:

class Item \< ActiveRecord::Base

belongs\_to :supplier, :foreign\_key =\> “supplier\_id”

end

---

<div class="post-metadata">

**Author:** ![rubyrubyruby\_onrail](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@rubyrubyruby\_onrail](https://www.ruby-forum.com/u/rubyrubyruby_onrail)\
**Post date:** [June 14, 2007, 11:50am UTC](https://www.ruby-forum.com/t/problem-foreign-key-in-insert-is-null/106818/4 "2007-06-14T11:50:21Z")

</div>

in the controller Items#create:

did you assign the association? maybe becuase of the relashinship, rails  
doesn’t look at the column as a regular attribute, and you need to do  
this manually, such as

def create  
…  
…  
@item.supplier\_id=params[:record][:supplier\_id]  
…  
…  
@item.save  
…  
end

?

---

<div class="post-metadata">

**Author:** ![rubyrubyruby\_onrail](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7feea3/32.png) [@rubyrubyruby\_onrail](https://www.ruby-forum.com/u/rubyrubyruby_onrail)\
**Post date:** [June 14, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/problem-foreign-key-in-insert-is-null/106818/5 "2007-06-14T12:46:00Z")

</div>

Shai, you’re a genius. I’ve had that issue for days but no longer, and  
if you’ll pardon the pun, I’m finally back on track (rails - awful).

Many thanks,

C
