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

Thank you, this was helpful for me. I still don't, frankly, recall (did I ever know) how Rails implements the latter without the former.


No problem!

The summary is basically this: if you say that a User has_many foos, then the foos table will have a user_id in it. When you ask for the foos a user has, the SQL will be emitted that has the "user_id=1" or whatever clause on it.

If you add a foreign key constraint, the database itself will verify that the foos have valid user_ids. If you don't, then it won't. You're basically giving up the ability for the database to verify the relationship for you. Like any kind of checks, this is more restrictive, but you get some value out of it. The question is, is the validation worth the restrictiveness. The OP comment argues no. Many would argue yes. It really just depends.


Yes, the word "constraint" is confusingly absent (or, rather, used colloquially) in the OP.

> FKs impose a lot of constraints on what's possible and what's not possible.

Thanks again for the further clarification.




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

Search: