In a StackOverflow clone, what relationship should a Comments table have to Questions and Answers?

I’d go with the Posts approach. This is the best way to ensure referential integrity. If you need additional columns for Answers and Questions respectively, put them in additional tables with a one-to-one relationship with Posts. For example, in MySQL syntax: CREATE TABLE Posts ( post_id SERIAL PRIMARY KEY, post_type CHAR(1), — must be ‘Q’ … Read more

How to implement polymorphic associations in an existing database

You could use Option 1 but without an additional surrogate Alternate Key. Instead, extend the existing Primary Key (of each entity), with an EntityType column (say CHAR(1), that would be E for Events, P for Persons, D for Products). The compound (EntityId, EntityType) will become then the Primary Key of table Entity and the corresponding … Read more

What is the best way to implement Polymorphic Association in SQL Server?

Another common Name for this model is the Supertype Model, where one has a base set of attributes that can be expanded via joining to another entity. In Oracle books, it is taught both as a logical model and physical implementation. The model without the relations would allow data to grow into invalid state and … Read more

Rails Polymorphic Association with multiple associations on the same model

I have done that in my project. The trick is that photos need a column that will be used in has_one condition to distinguish between primary and secondary photos. Pay attention to what happens in :conditions here. has_one :photo, :as => ‘attachable’, :conditions => {:photo_type => ‘primary_photo’}, :dependent => :destroy has_one :secondary_photo, :class_name => ‘Photo’, … Read more

accepts_nested_attributes_for with belongs_to polymorphic

I’ve also had a problem with the “ArgumentError: Cannot build association model_name. Are you trying to build a polymorphic one-to-one association?” And I found a better solution for this kind of problem. You can use native method. Lets look to the nested_attributes implementation, inside Rails3: elsif !reject_new_record?(association_name, attributes) method = “build_#{association_name}” if respond_to?(method) send(method, attributes.except(*UNASSIGNABLE_KEYS)) … Read more

ActiveRecord, has_many :through, and Polymorphic Associations

There is a known issue with Rails 3.1.1 that breaks this functionality. If you are having this problem first try upgrading, it’s been fixed in 3.1.2 You’re so close. The problem is you’re misusing the :source option. :source should points to the polymorphic belongs_to relationship. Then all you need to do is specify :source_type for … Read more

MySQL – Conditional Foreign Key Constraints

You’re attempting to do a design that is called Polymorphic Associations. That is, the foreign key may reference rows in any of several related tables. But a foreign key constraint must reference exactly one table. You can’t declare a foreign key that references different tables depending on the value in another column of your Comments … Read more

Why can you not have a foreign key in a polymorphic association?

A foreign key must reference only one parent table. This is fundamental to both SQL syntax, and relational theory. A Polymorphic Association is when a given column may reference either of two or more parent tables. There’s no way you can declare that constraint in SQL. The Polymorphic Associations design breaks rules of relational database … Read more