Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 248607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:21:26+00:00 2026-05-11T21:21:26+00:00

In an application similar to StackOverflow that I am building, I am trying to

  • 0

In an application similar to StackOverflow that I am building, I am trying to decide what relationship my Questions, Answers and Comments tables should have.

I could have Questions and Answers both be represented by a single table Posts.

That would allow Comments to have a single foreign key to Posts.

But if Questions and Answers are separate tables, what relationships should Comments have to each of these?

UPDATE: Although the chosen answer recommends a Class Table Inheritance approach and this seems like the best approach in database terms, this option is not supported by the Rails ORM. So, in Rails my models will have to use Single Table Inheritance and will probably look like this:

class Post < ActiveRecord::Base  
end  

class Question < Post  
  has_many :answers, :foreign_key => :parent_id  
  has_many :comments, :foreign_key => :parent_id  
end  

class Answer < Post  
  belongs_to :question, :foreign_key => :parent_id  
  has_many :comments, :foreign_key => :parent_id  
end  

class Comment < Post  
  belongs_to :question, :foreign_key => :parent_id  
  belongs_to :answer, :foreign_key => :parent_id  
end


class CreatePosts < ActiveRecord::Migration  
    def self.up  
      create_table :posts do |t|  
        t.string :type 
        t.string :author   
        t.text :content  
        t.integer :parent_id   
        t.timestamps  
      end  
    end  


    def self.down  
      drop_table :posts  
    end  
end
CREATE TABLE "posts" (
  "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,  
  "type" varchar(255),  
  "author" varchar(255),  
  "content" text,  
  "parent_id" integer,  
  "created_at" datetime, 
  "updated_at" datetime
  );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:21:26+00:00Added an answer on May 11, 2026 at 9:21 pm

    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' or 'A'
      -- other columns common to both types of Post
      UNIQUE KEY (post_id, post_type) -- to support foreign keys
    ) ENGINE=InnoDB;
    
    CREATE TABLE Comments (
      comment_id  SERIAL PRIMARY KEY, 
      post_id     BIGINT UNSIGNED NOT NULL,
      -- other columns for comments (e.g. date, who, text)
      FOREIGN KEY (post_id) REFERENCES Posts(post_id)
    ) ENGINE=InnoDB; 
    
    CREATE TABLE Questions (
      post_id     BIGINT UNSIGNED PRIMARY KEY,
      post_type   CHAR(1),              -- must be 'Q'
      -- other columns specific to Questions
      FOREIGN KEY (post_id, post_type) REFERENCES Posts(post_id, post_type)
    ) ENGINE=InnoDB;
    
    CREATE TABLE Answers (
      post_id     BIGINT UNSIGNED PRIMARY KEY,
      post_type   CHAR(1),              -- must be 'A'
      question_id BIGINT UNSIGNED NOT NULL,
      -- other columns specific to Answers
      FOREIGN KEY (post_id, post_type) REFERENCES Posts(post_id, post_type)
      FOREIGN KEY (question_id) REFERENCES Questions(post_id)
    ) ENGINE=InnoDB;
    

    This is called Class Table Inheritance. There’s a nice overview of modeling inheritance with SQL in this article: “Inheritance in relational databases.”

    It can be helpful to use post_type so a given Post can be only one answer or one question. You don’t want both an Answer and a Question to reference one given Post. So this is the purpose of the post_type column above. You can use CHECK constraints to enforce the values in post_type, or else use a trigger if your database doesn’t support CHECK constraints.

    I also did a presentation that may help you. The slides are up at http://www.slideshare.net/billkarwin/sql-antipatterns-strike-back. You should read the sections on Polymorphic Associations and Entity-Attribute-Value.


    If you use Single Table Inheritance, as you said you’re using Ruby on Rails, then the SQL DDL would look like this:

    CREATE TABLE Posts (
      post_id     SERIAL PRIMARY KEY,
      post_type   CHAR(1),              -- must be 'Q' or 'A'
      -- other columns for both types of Post
      -- Question-specific columns are NULL for Answers, and vice versa.
    ) ENGINE=InnoDB;
    
    CREATE TABLE Comments (
      comment_id  SERIAL PRIMARY KEY, 
      post_id     BIGINT UNSIGNED NOT NULL,
      -- other columns for comments (e.g. date, who, text)
      FOREIGN KEY (post_id) REFERENCES Posts(post_id)
    ) ENGINE=InnoDB; 
    

    You can use a foreign key constraint in this example, and I recommend that you do! 🙂

    Rails philosophy tends to favor putting enforcement of the data model into the application layer. But without constraints enforcing integrity at in the database, you have the risk that bugs in your application, or ad hoc queries from a query tool, can harm data integrity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Delphi application similar to Taskbar Shuffle that includes a hook dll.
I am building an application that is very similar to a shopping cart. The
I have an application that works some what similar to how iPhone's Contact application
My web application is similar to StackOverflow in that different users frequently edit the
I have a large application (~50 modules) using a structure similar to the following:
I have code similar to this in my application: class A { public: int
I have an asp.net mvc application with a route similar to: routes.MapRoute(Blog, {controller}/{action}/{year}/{month}/{day}/{friendlyName}, new
I'm writing an application that needs a log-like view, (similar to how an IM
I'm currently in the middle of developing an application loosely similar to StackOverflow (question
Our application has a file format similar to the OpenDocument file format (see http://en.wikipedia.org/wiki/OpenDocument

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.