在 Rails 4 中添加引用列迁移

编程入门 行业动态 更新时间:2024-10-27 07:15:52
本文介绍了在 Rails 4 中添加引用列迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

一个用户有很多上传.我想向引用 user 的 uploads 表添加一列.迁移应该是什么样的?

A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like?

这是我所拥有的.我不确定我是否应该使用 (1) :user_id, :int 或 (2) :user, :references.我什至不确定(2)是否有效.只是想以轨道"的方式做到这一点.

Here is what I have. I'm not sure if I should use (1) :user_id, :int or (2) :user, :references. I'm not even sure if (2) works. Just trying to do this the "rails" way.

class AddUserToUploads < ActiveRecord::Migration def change add_column :uploads, :user_id, :integer end end

Rails 3 以外的相关问题.Rails 3 迁移:添加参考列?

Relevant question except for Rails 3. Rails 3 migrations: Adding reference column?

推荐答案

Rails 4.x

当您已经users和上传表并希望在它们之间添加新关系时.

When you already have users and uploads tables and wish to add a new relationship between them.

您需要做的就是:只需使用以下命令生成迁移:

All you need to do is: just generate a migration using the following command:

rails g migration AddUserToUploads user:references

这将创建一个迁移文件:

Which will create a migration file as:

class AddUserToUploads < ActiveRecord::Migration def change add_reference :uploads, :user, index: true end end

然后,使用 rake db:migrate 运行迁移.此迁移将负责将名为 user_id 的新列添加到 uploads 表(引用 users 表中的 id 列),此外它还会在新列上添加索引.

Then, run the migration using rake db:migrate. This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.

更新 [对于 Rails 4.2]

无法信任 Rails 来维护引用完整性;关系数据库在这里帮助我们.这意味着我们可以在数据库级别本身添加外键约束,并确保数据库拒绝任何违反此集合参照完整性的操作.正如@infoget 评论的那样,Rails 4.2 附带对外键(参照完整性) 的本机支持.这不是必需的,但您可能希望将外键(因为它非常有用)添加到我们上面创建的引用中.

Rails can’t be trusted to maintain referential integrity; relational databases come to our rescue here. What that means is that we can add foreign key constraints at the database level itself and ensure that database would reject any operation that violates this set referential integrity. As @infoget commented, Rails 4.2 ships with native support for foreign keys(referential integrity). It's not required but you might want to add foreign key(as it's very useful) to the reference that we created above.

要将外键添加到现有引用,请创建新迁移以添加外键:

To add foreign key to an existing reference, create a new migration to add a foreign key:

class AddForeignKeyToUploads < ActiveRecord::Migration def change add_foreign_key :uploads, :users end end

要使用外键创建一个全新的引用(在 Rails 4.2 中),请使用以下命令生成迁移:

To create a completely brand new reference with a foreign key(in Rails 4.2), generate a migration using the following command:

rails g migration AddUserToUploads user:references

这将创建一个迁移文件:

which will create a migration file as:

class AddUserToUploads < ActiveRecord::Migration def change add_reference :uploads, :user, index: true add_foreign_key :uploads, :users end end

这将向 uploads 表的 user_id 列添加一个新的外键.键引用 users 表中的 id 列.

This will add a new foreign key to the user_id column of the uploads table. The key references the id column in users table.

注意:这是除了添加引用之外,因此您仍然需要先创建引用,然后创建外键(您可以选择创建外键键在同一个迁移或单独的迁移文件中).Active Record 仅支持单列外键,目前仅支持 mysql、mysql2 和 PostgreSQL 适配器.不要在 sqlite3 等其他适配器上尝试此操作.请参阅 Rails Guides: Foreign键供您参考.

NOTE: This is in addition to adding a reference so you still need to create a reference first then foreign key (you can choose to create a foreign key in the same migration or a separate migration file). Active Record only supports single column foreign keys and currently only mysql, mysql2 and PostgreSQL adapters are supported. Don't try this with other adapters like sqlite3, etc. Refer to Rails Guides: Foreign Keys for your reference.

更多推荐

在 Rails 4 中添加引用列迁移

本文发布于:2023-10-24 15:01:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!