shiva
shiva

Reputation: 434

Is good idea to create two separate tables which have similar structure in postgres?

I'm working on rails application with postgres db.I have a table called merchant_review_votes where schema is

--------------------------------------------------
 id                  | integer 
 user_id             | integer 
 merchant_review_id  | integer
 value               | integer
 created_at          | timestamp without time zone
 updated_at          | timestamp without time zone
--------------------------------------------------

I'm creating a another section(say products) which involve voting is it a good idea to create another schema similar to this or modify the code and use just one table. If just one table how will the schema look?. What kinda problems i can face..

Upvotes: 0

Views: 180

Answers (3)

Chris Travers
Chris Travers

Reputation: 26454

This is probably too open-ended generally for SO, but here;s my take: it depends!

Sometimes it makes sense. I am not a fan of, say, global notes tables.

In general, I think it is worth avoiding this but when you have to, having both tables inherit a common base with no records vastly simplifies management.

Upvotes: 0

Nate
Nate

Reputation: 16898

This sounds like a good case for a Polymorphic Association.

class Review < ActiveRecord::Base
  belongs_to :user
  belongs_to :reviewable, :polymorphic => true
end

class Merchant < ActiveRecord::Base
  has_many :reviews, :as => :reviewable
end

class Product < ActiveRecord::Base
  has_many :reviews, :as => :reviewable
end

You can then call @merchant.reviews and @product.reviews to get the reviews of merchants and products, respectively. And you can call @review.reviewable to get the object that was reviewed.

The polymorphic association is based on a table structure like this:

--------------------------------------------------
  id                  | integer 
  user_id             | integer 
  reviewable_id       | integer
  reviewable_type     | string
  value               | integer
  created_at          | timestamp without time zone
  updated_at          | timestamp without time zone
--------------------------------------------------

The reviewable_type column holds the class name of the "reviewable" object and the reviewable_id holds it's id. In a migration you can create these columns using:

 t.references :reviewable, :polymorphic => true

Upvotes: 0

user330315
user330315

Reputation:

If both reviews contain exactly the same information, then I'd go for a single table that has a "review_type" column. For store reviews that column would contain 'store' for product reviews that column would contain 'product'.

But I'm missing a reference to the stored that was reviewed in your table definition. If you need to have that (which is very likely, otherwise you wouldn't know to which store or product the review belongs), you are probably better of having two tables. One with a foreign key to the store table, one with a foreign key to the product table.

Upvotes: 1

Related Questions