【发布时间】:2010-10-28 14:42:35
【问题描述】:
我在 Post 类中有一个带有 post_id 的 :foreign_key 的 Comment 类。
class Comment < ActiveRecord::Base
belongs_to :post, :class_name => "Post", :foreign_key => "post_id", :counter_cache => true
belongs_to :author, :class_name => "User", :foreign_key => "author_id"
end
但我的 CreateComments 迁移没有定义数据库级外键:
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.column "post_id", :integer, :default => 0, :null => false
t.column "author", :string, :default => "", :limit => 25, :null => false
t.column "author_email", :string, :default => "", :limit => 50, :null => false
t.column "content", :text, :null => false
t.column "status", :string, :default => "", :limit => 25, :null => false
t.timestamps
end
end
def self.down
drop_table :comments
end
end
相反,post_id 是一个简单的整数列。
所以,这种外键关系似乎只存在于 Rails 的头脑中,而不存在于数据库层面。
这对吗?
另外,相应的 Post 模型是否也需要使用 :foreign_key 属性声明其与 Comments 的互惠外键关系,或者可以省略吗?
class Post < ActiveRecord::Base
set_table_name("blog_posts")
belongs_to :author, :class_name => "User", :foreign_key => 'author_id'
has_many :comments, :class_name => "Comment",
:foreign_key => 'post_id', :order => "created_at desc", :dependent => :destroy
has_many :categorizations
has_many :categories, :through => :categorizations
named_scope :recent, :order => "created_at desc", :limit => 5
end
【问题讨论】:
-
我和你一样惊讶的是 Rails 不使用 SQL 外键。这使得在 DB 上使用非 Rails 工具变得很困难。
-
Rails 遵循“所有业务逻辑都应在应用程序中定义”的约定......因此它仅将 DB 用作“哑”存储。没有外键,没有存储过程,没有约束(例如在 postgres 中支持)。编辑:刚刚发现这个答案是一样的 - stackoverflow.com/questions/8334602/…
标签: ruby-on-rails foreign-keys