Skip to content

Comment on Rails 4.2 releasedparent

Comments

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
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.