创建join table时是否需要在individual table中声明外键?
Do I need a foreign key declaration in the individual table when creating join table?
我有一个名为 content_roles 的 "contents" 和 "roles" 的连接 table,这就是连接 table。
class CreateContentRoles < ActiveRecord::Migration
def change
create_table :content_roles, :id => false do |t|
t.belongs_to :content, foreign_key: "content_id"
t.belongs_to :roles, foreign_key: "role_id"
end
add_index :content_roles, ["content_id", "roles_id"]
end
end
所以在个人角色和内容迁移中,我是否需要有一个 foreign_key 来引用回加入 table and/or 和 roles/contents?对不起,我没有更好地解释这个。
content
和 roles
table 中的 id
列用作外键。 Rails 加入 content
和 content_roles
table 使用 id
in content
table 和 content_id
in content_roles
table。 roles
和 content_roles
table 也是如此。
为什么不能使用如下所示的引用
class CreateContentRoles < ActiveRecord::Migration
def change
create_table :content_roles, :id => false do |t|
t.references :content, index: true, foreign_key: true
t.references :roles, index: true, foreign_key: true
t.timestamps null: false
end
end
end
我有一个名为 content_roles 的 "contents" 和 "roles" 的连接 table,这就是连接 table。
class CreateContentRoles < ActiveRecord::Migration
def change
create_table :content_roles, :id => false do |t|
t.belongs_to :content, foreign_key: "content_id"
t.belongs_to :roles, foreign_key: "role_id"
end
add_index :content_roles, ["content_id", "roles_id"]
end
end
所以在个人角色和内容迁移中,我是否需要有一个 foreign_key 来引用回加入 table and/or 和 roles/contents?对不起,我没有更好地解释这个。
content
和 roles
table 中的 id
列用作外键。 Rails 加入 content
和 content_roles
table 使用 id
in content
table 和 content_id
in content_roles
table。 roles
和 content_roles
table 也是如此。
为什么不能使用如下所示的引用
class CreateContentRoles < ActiveRecord::Migration
def change
create_table :content_roles, :id => false do |t|
t.references :content, index: true, foreign_key: true
t.references :roles, index: true, foreign_key: true
t.timestamps null: false
end
end
end