Add a reference column migration in Rails 5

跟風遠走 提交于 2020-02-20 06:21:09

问题


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?

Related question for Rails 3: Rails 3 migrations: Adding reference column?

Related question for Rails 4: Add a reference column migration in Rails 4


回答1:


As with prior versions of Rails, you may use the following command to create the migration:

rails g migration AddUserToUploads user:references

Unlike prior versions of Rails, the migration looks like:

class AddUserToUploads < ActiveRecord::Migration[5.0]
  def change
    add_reference :uploads, :user, foreign_key: true
  end
end


来源:https://stackoverflow.com/questions/39937839/add-a-reference-column-migration-in-rails-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!