I'm getting confused, are references and foreign keys the same thing
really?
Is there a good example on FKs?
I'd like to see how it goes in practice, for example how deleting the
parent record would delete any existing records depending on it, and
also how to avoid that from occurring...
I'm getting confused, are references and foreign keys the same thing
really?
Sort of. I suspect what you are actually wondering about are foreign
key constraints (where the database enforces that each foo_id column
does indeed contain the id of a row from the foos table.
Rails doesn't provide anything builtin for doing this, although there
are various plugins.
The documentation for your database should provide information on how
it handles foreign keys
Fred
Yes you are right I'm after FK constraints. And I guess that a way to do
that is for example using this in a migration file:
execute "alter table user_images add constraint fk_wi_user foreign key
(user_id) references users(id)"
I think I understand it a bit better now.
Using references or something like t.integer :project_id help creating
the relations between tables, and help with queries like SELECT * FROM
projects WHERE user_id = 22;
The foreign key constrains help using those keys to keep DB data
consistent for example. As I mentioned I was actually looking for ON
DELETE CASCADE so that was missing on my constraint definition above. It
should be: