How to get Ruby on Rails to create a SQLite database with foreign keys enabled?

I'm new to Ruby on Rails, and I'm using SQLite3 as a database for my sample project. I am trying to create the usual relationship between two models (for example, each product has one Owner, and each Owner can have many products). This works great, and the database schema is created correctly. However, when I open development.sqlite3 in the database management tool (I use the free SQLite Express Personal http://www.sqliteexpert.com/download.html ) I do not see that the database has referential integrity. There are no foreign keys for the Product table, even if they contain owner_id columns .

I tried changing database.yml by adding an options key:

default: &default
adapter: sqlite3
pool: 5
timeout: 5000
options: "PRAGMA foreign_keys=ON"

And then recreate the database with:

rake db:drop db:create db:migrate

This recreates the database, but again there are no foreign keys.

Am I doing something wrong? Is there any solution for this at all. (PS. I run it all on Windows 8.1, if that matters)

+4
source share
1 answer

By default, Rails does not create foreign key references. Rails manages foreign key values ​​using information obtained from associations in models.

Rails 4.2 add_foreign_key migrations. SQLite, SQLite ALTER TABLE, Rails . SQLite ALTER TABLE.

ALTER TABLE, DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT .. .

, add_foreign_key .

Rails , dbms, : a) - PostgreSQL MySQL, b) SQL . - PostgreSQL, - ALTER TABLE.

, , dbms. , Rails Rails.


Rails 4.2 , add_foreign_key SQLite.

DSL . O schema.rb. mysql, mysql2 postgresql .

+6

All Articles