为什么在多态关联中没有外键?

为什么在多态关联中没有外键,例如下面表示为Rails模型的外键?

class Comment < ActiveRecord::Base
  belongs_to :commentable, :polymorphic => trueendclass Article < ActiveRecord::Base
  has_many :comments, :as => :commentableendclass Photo < ActiveRecord::Base
  has_many :comments, :as => :commentable  #...endclass Event < ActiveRecord::Base
  has_many :comments, :as => :commentableend


九州编程
浏览 789回答 3
3回答

三国纷争

究竟。当Polymorphic Associations文档本身表示你不能使用外键约束时,它应该是一个强大的“代码味道”,它不是正确的关系数据库设计!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Ruby
SQL Server