为什么在多态关联中没有外键,例如下面表示为Rails模型的外键?
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Photo < ActiveRecord::Base
has_many :comments, :as => :commentable
#...
end
class Event < ActiveRecord::Base
has_many :comments, :as => :commentable
end
Run Code Online (Sandbox Code Playgroud) database ruby-on-rails foreign-key-relationship polymorphic-associations
我是Ruby on Rails的新手(虽然我很熟悉Ruby)并且看着Migration工具,听起来真的很棒.数据库模式最终(很容易)进入源代码管理.
现在我的问题.使用Postgres作为数据库时,它不会设置外键.我想在我的架构中使用外键的好处,例如引用完整性.那么如何在迁移中应用外键?