class CreateCustomers < ActiveRecord::Migration
def change
create_table :customers do |t|
t.belongs_to :user
t.string :firstname
t.string :surname
t.timestamps
end
end
这些是我的模型:
class Customer < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :customers
end
找到t.belongs_to :user
在数据库中添加外键。
但是当我用 pgadminIII 检查我的 postgre 数据库时,没有列出 foreign_key。
迁移会按原样添加 user_id,但没有列出 foreign_key。
有什么解释吗?
最好的问候 拒绝
编辑:关于 t.belongs_to
的更多信息
create_table :accounts do |t|
t.belongs_to :supplier
t.string :account_number
t.timestamps
end
这是其中的一个例子。来自 railsguide 电子书第 2 章。
最佳答案
这是我第一次看到这个 belongs_to
东西,但是要在迁移中的列上添加索引,你需要写
t.belongs_to :user, index: true
(注意新的哈希语法)
关于ruby-on-rails - t.belongs_to 和对 postgresql 数据库的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578803/