Ale*_*tko 5 ruby-on-rails ruby-on-rails-4
我目前正在使用 Rails 4.2.4。问题是当我跑的时候
rails g migration AddCategoryRefToArticles category:references命令,
它生成了以下迁移
def change
add_reference :articles, :category, index: true, foreign_key: true
end
Run Code Online (Sandbox Code Playgroud)
由于某种原因,结果category_id是整数字段,而不是预期的 t.references。
create_table "articles", force: :cascade do |t|
t.string "title"
t.integer "category_id"
end
add_index "articles", ["category_id"], name: "index_articles_on_category_id", using: :btree
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?
add_reference只是一个方便的帮助程序,用于生成遵循关联中使用的命名约定的整数字段。由于schema.rb映射了数据库模式,因此您应该看到特定的数据类型而不是更高级别的抽象。
我不确定您为什么会期望 a t.references,但您的期望是错误的。文档中也对此进行了解释add_reference。
创建
user_id整数列Run Code Online (Sandbox Code Playgroud)add_reference(:products, :user)
| 归档时间: |
|
| 查看次数: |
1004 次 |
| 最近记录: |