The foreigner
README clearly states
The following adapters are supported:
- sqlite (foreign key methods are a no-op)
So your SQLite database does not have foreign key constraints set up because foreigner
doesn't support them. When db/schema.rb
is generated from the SQLite database, this is why there are no foreign keys specified.
The Rails Guide on Migrations mentions foreign keys quite a bit
If you need to perform tasks specific to your database (for example create a foreign key constraint) then the
execute
method allows you to execute arbitrary SQL
There is even an example of how to add/remove a foreign key.
Having used foreigner
when first starting out with Rails, I suggest you drop it from your Gemfile
and either
- Don't use foreign keys at all; just specify your cascades on your ActiveRecord associations
- Use the
execute
migration method as described in the linked example above (and just make sure all the different RDBMS' support whatever you put into theexecute
methods) - Stop using SQLite in development if you insist on using foreign keys. You're not going to use SQLite in production anyway, and are already using better RDMS (MySQL, "better" in the sense that it has the foreign key support you are looking for) on other machines.
As you pointed out in the comments, SQLite has lacking support for adding foreign keys after the table has been created; they cannot be added through a futre migration in Rails. I personally suggest you use choice 1 or 3, as it is going to be more difficult to create a solution through execute
commands in migrations that satisfies the restrictions of SQLite while having the same end result on other RDMS'.