# Removing a default value for a foreign key with not null set

**URL:** <https://www.ruby-forum.com/t/removing-a-default-value-for-a-foreign-key-with-not-null-set/58930>\
**Category:** Rails\
**Created:** [April 27, 2006, 8:03am UTC](https://www.ruby-forum.com/t/removing-a-default-value-for-a-foreign-key-with-not-null-set/58930 "2006-04-27T08:03:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [April 27, 2006, 8:03am UTC](https://www.ruby-forum.com/t/removing-a-default-value-for-a-foreign-key-with-not-null-set/58930/1 "2006-04-27T08:03:06Z")

</div>

Hi all,

For various reasons I need to have foreign keys with not null  
constraints.  
Eg:

create\_table :people do |t|  
t.column :name, :string  
t.column :household\_id, :integer, :null =\> false  
end

create\_table :households do |t|  
t.column :name, :string  
end

add\_foreign\_key\_constraint :people, :household\_id, :households, :id

So the people table has a foreign key into households (household\_id),  
and  
not null is enabled on the column.

My problem is that I get default values of 0 for household\_id when  
instantiating new Person object.

Person.new.household\_id # == 0

validates\_presence\_of :household\_id then passes because of the 0 value,  
and  
the insert fails because 0 is not a valid foreign key.

A simple fix is to use a before\_validation and remove and 0 values for  
\_id  
fields, but I’m looking for a cleaner solution:

class Person \< ActiveRecord::Base  
belongs\_to :household  
validates\_presence\_of :household\_id

def before\_validation  
attributes.each do |key, value|  
self[key] = nil if value == 0 and key.ends\_with?(’\_id’)  
end  
end  
end

Is there a better way?

Cheers, Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [April 27, 2006, 8:15am UTC](https://www.ruby-forum.com/t/removing-a-default-value-for-a-foreign-key-with-not-null-set/58930/2 "2006-04-27T08:15:19Z")

</div>

Hi Jonathan

Put a default value of null (or nil in migrations I suppose) in your DB  
setup

Then there is no worries about removing it 🙂

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [April 27, 2006, 11:24am UTC](https://www.ruby-forum.com/t/removing-a-default-value-for-a-foreign-key-with-not-null-set/58930/3 "2006-04-27T11:24:23Z")

</div>

Unfortunately it’s not that simple. When you’ve specified NOT NULL in  
Mysql  
, you can’t give the column a default of null.

ERROR 1067 (42000): Invalid default value for ‘person\_id’

That’s fair enough because you’ve said you don’t want nulls allowed, and  
then asked for a default of null.

Any other suggestions?

-Jonathan.

---

<div class="post-metadata">

**Author:** ![jonny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonny](https://www.ruby-forum.com/u/jonny)\
**Post date:** [April 27, 2006, 2:51pm UTC](https://www.ruby-forum.com/t/removing-a-default-value-for-a-foreign-key-with-not-null-set/58930/4 "2006-04-27T14:51:55Z")

</div>

Ok Sorry I missed the first part… My bad

I think you can use another validation here.

The validates\_associated method checks the validity of the associated  
model. Note it should be used after a call to validates\_presence\_of

The docs can be found at

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000820](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000820)

Please see below in the model definition.

On 4/27/06, Jonathan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Then there is no worries about removing it 🙂  
> > t.column :name, :string  
> > and not null is enabled on the column.  
> > \_id fields, but I’m looking for a cleaner solution:
> > 
> > class Person \< ActiveRecord::Base  
> > belongs\_to :household  
> > validates\_presence\_of :household\_id
> 
> ```
> validates_associated :household_id
> 
> ```

```
   end

```
