简体   繁体   English

如何使Ruby on Rails创建启用了外键的SQLite数据库?

[英]How to make Ruby on Rails create SQLite database with foreign keys enabled?

I am newbie to Ruby on Rails and I am using SQLite3 as database for my sample project. 我是Ruby on Rails的新手,我使用SQLite3作为我的示例项目的数据库。 I am trying to create ordinary one to many relationship between two models (eg. each Product has one Owner, and each Owner can have many Products). 我试图在两个模型之间创建普通的一对多关系(例如,每个产品有一个所有者,每个所有者可以有许多产品)。 This works fine and database schema is created properly. 这很好,并且正确创建了数据库模式。 However, when I open development.sqlite3 in database management tool (I use free SQLite Express Personal http://www.sqliteexpert.com/download.html ) I don't see that database has referential integrity. 但是,当我在数据库管理工具中打开development.sqlite3时(我使用免费的SQLite Express Personal http://www.sqliteexpert.com/download.html )我没有看到该数据库具有参照完整性。 There are no foreign keys listed for Product table even though it does contain owner_id column. 即使包含owner_id列,也没有为Product表列出外键。

I tried changing database.yml by adding options key: 我尝试通过添加选项键来更改database.yml:

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

And then recreate database with: 然后重新创建数据库:

rake db:drop db:create db:migrate

This recreates database but again there is no foreign keys listed. 这会重新创建数据库,但同样没有列出外键。

Am I doing something wrong? 难道我做错了什么? Is there solution for this at all. 是否有解决方案。 (PS. I am running all this on Windows 8.1 if that matters) (PS。如果这很重要,我在Windows 8.1上运行所有这些)

By default, Rails doesn't create foreign key references. 默认情况下,Rails不会创建外键引用。 Rails manages foreign key values itself, using information it derives from associations in the models. Rails的管理外键值本身,用它从派生的信息关联的模型。

Rails 4.2 supports add_foreign_key in migrations . Rails 4.2迁移中 支持add_foreign_key But you can't use that with SQLite, because SQLite doesn't support the specific ALTER TABLE statements that Rails uses to create foreign keys. 但是你不能在SQLite中使用它,因为SQLite不支持Rails用来创建外键的特定ALTER TABLE语句。 SQLite has very limited support for ALTER TABLE . SQLite 对ALTER TABLE的支持非常有限

Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted. 省略了其他种类的ALTER TABLE操作,例如DROP COLUMN,ALTER COLUMN,ADD CONSTRAINT等。

Also, add_foreign_key supports only foreign key references to a single column. 此外,add_foreign_key仅支持对单个列的外键引用。

If you want to learn Rails with referential integrity enforced by the dbms, you'll need to either a) switch to something like PostgreSQL or MySQL, or b) execute raw SQL in migrations. 如果您想了解Rails的 DBMS强制引用完整性,你需要到任何一个)切换到类似的PostgreSQL或MySQL,或b)在迁移执行原始的SQL。 Even this will be easier with something like PostgreSQL, mainly because of its full support for standard ALTER TABLE statements. 使用像PostgreSQL这样的东西会更容易,主要是因为它完全支持标准的ALTER TABLE语句。

If I were you, I wouldn't worry about foreign keys being enforced by the dbms yet. 如果我是你,我不会担心dbms强制执行外键。 Wait until you're comfortable with the Rails environment and Rails conventions first. 等到你对Rails环境和Rails约定感到满意为止。


The release notes for Rails 4.2 also say, more or less, that add_foreign_key doesn't work with SQLite yet. Rails 4.2的发行说明或多或少地说,add_foreign_key还不能与SQLite一起使用。

The migration DSL now supports adding and removing foreign keys. 迁移DSL现在支持添加和删除外键。 They are dumped to schema.rb as well. 它们也被转储到schema.rb。 At this time, only the mysql, mysql2 and postgresql adapters support foreign keys. 这时,只有mysql,mysql2和postgresql适配器支持外键。

Here is my attempt to clear this in Rails 5.x documentation: 这是我尝试在Rails 5.x文档中清除它:

https://github.com/rails/rails/pull/33563 https://github.com/rails/rails/pull/33563

At this time, only the mysql, mysql2 and postgresql adapters support foreign keys. 这时,只有mysql,mysql2和postgresql适配器支持外键。 Implementation for sqlite3 is partial, keys are created for new tables but not for existing tables via ALTER TABLE statement. sqlite3的实现是部分的,为新表创建密钥,但不是通过ALTER TABLE语句为现有表创建密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM