Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Using `belongs_to` or `references` in a migration is simply an alias for an integer column (that conventionally holds the id of another row) without any foreign key constraints.

Foreign key constraints aren't about performance - they are about maintaining [referential integrity].

previously...

    pet.id # => 4
    owner.update_attribute(:pet_id, 4)
    pet.destroy
    owner.pet # what should this equal?

Now this will throw an error.

    pet.id # => 4
    owner.update_attribute(:pet_id, 4)
    pet.destroy # <= raises Error:trying to destroy a record that is referenced by the owners table.

    owner.update_attribute(:pet_id, nil)
    pet.destroy # no problem now.

[referential integrity] http://en.wikipedia.org/wiki/Referential_integrity


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: