L
L
Lesha Fedoseev2014-01-05 17:49:08
PostgreSQL
Lesha Fedoseev, 2014-01-05 17:49:08

Rails + PostgreSQL: integer and decimal data in one table - combine into one column or separate into different ones?

My Rails engine using PostgreSQL imports statistics for several метрикамfrom Google Analytics:

  • Visitors-int
  • Goal 1 Completions-int
  • Goal 2 Completions-int
  • Goal N Completions-int
  • Transactions Amount -int
  • TransactionsRevenue-decimal

This is an engine that will be mounted to different client sites. And in the future, apart from goals and e-commerce data, anything can be there (for example, social metrics, etc.). Therefore, my task is to make the architecture as flexible as possible: metric types should not be added by changing the database architecture, but by adding new types to the metrics directory (there is a diagram visualization just below).
This is how a table with statistics on metrics in the database would look like if all metrics were int:
  • id -int
  • date-date
  • metric_id -int
  • metric_amount -int

But there is Transactions Revenue (money), which is decimal, and I have 2 ways:
Way #1. Make all metric_amount decimal
  • id -int
  • date-date
  • metric_id -int
  • metric_amount -decimal
Path #2. Add nullable column for Revenue
  • id -int
  • date-date
  • metric_id -int
  • metric_amount -int
  • metric_revenue-decimal

An example path #2 looks like this:
w8y.png
Which option is correct? Or are both wrong and is there a better solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
romanwb, 2014-01-06
@romanwb

Sometimes, when using polymorphic relationships, one table may have different fields that are used by different models.
So, the second option with int and decimal is quite logical.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question