15

So we all know that rails' STI (single table inheritance) is icky because it leads to a cluttered data model and suboptimal database.

However PostgreSQL seems to handle inheritance quite beautifully!

Is there a way to get rails' nice clean STI API while utilizing Postgres inheritance instead of painfully wide tables and "type" columns?

3
  • 1
    "So we all know that rails' STI (single table inheritance) is icky because it leads to a cluttered data model and suboptimal database." --- I do not accept this premise. Commented Apr 9, 2012 at 1:58
  • Okay maybe that's a bit of a generalisation.. But it tends to only be a good idea when your child models do not have lots of properties that do not apply to the other children.. Otherwise you end up with massively wide tables full of nullable columns. May be okay from a rails perspective but can get kinda ugly when running raw SQL on the database, perhaps PostgreSQL's inheritance isn't any faster but at least it hides all that away from me! :P Commented Apr 9, 2012 at 9:02
  • Because Postgres' inheritance fits on so seamlessly, I don't suppose just having a type column would be good enough? From what I've seen with inheritance, SELECT * would return all the associated columns (and I guess all unassociated columns too :/... But your DB structure would be cleaner.
    – stuartc
    Commented Apr 13, 2012 at 12:40

2 Answers 2

8

However PostgreSQL seems to handle inheritance quite beautifully!

Really? Did you take a close look at the small print in the manual? For example:

  • Unique constraints (and hence also primary keys) are not possible over inheritance levels.
  • References to inherited tables and the base table do not mix. I.e. (taken roughly from the manual): capitals extends cities. Your address table wants to reference cities. It can. But the no address with a capital can be used.

These two things are very important in anything beyond a small "Hello World" project. So I cannot imagine, that anything productive can be implemented using PostgreSQL inheritance.

5
  • 1
    Actually as I understand it is quite useful for table sharding. But it's true that it's not suited for model-space inheritance as it now stands.
    – Divide
    Commented Nov 20, 2012 at 14:18
  • Even table sharding is not possible with this in most cases: If you have more than one unique constraint you are screwed. And since most frameworks use a surrogate key this means that you cannot even have one unique constraint for the application data.
    – A.H.
    Commented Nov 20, 2012 at 19:12
  • Of course if you're using surrogate keys then sharding often becomes harder. Psql docs have a nice example of sharding: postgresql.org/docs/9.1/static/ddl-partitioning.html
    – Divide
    Commented Nov 28, 2012 at 6:53
  • 1
    @Divide: I think, I repeat the original answer ;-) Did you take a close look at the small print at the bottom of that page? For me this does not look "nice" but quite ugly, error-prone and maintenance intensive. In most (if not all) enterprise applications I have developed table sharding ala PostgreSQL would be a no-go.
    – A.H.
    Commented Nov 28, 2012 at 19:28
  • Table inheritance's remaining utility is in querying across children like it's a dynamically generated union. Very niche scenario, but sometimes easier than updating an equivalent view.
    – inopinatus
    Commented May 23 at 4:15
6

In short - no there no nice clean STI API for what you are trying to accomplish as of right now.

I actually looked into that a about a year ago and came to a conclusion that it is not a good idea for several reasons:

  1. If you want to utilize PostgreSQL specific features - you are basically marrying yourself to that DB. Don't get me wrong PostgreSQL is a great DB and I've used it on a number of occasions, but you are going to be stuck with that DB and app design.
  2. Most likely if you start using DB specific features you'll either end up implementing them manually ( running some kind of commands on the DB or using GUI) or writing some kind of script that you will have to invoke whenever you are running db:migrate ( you will have to do it if you want to do proper testing). After awhile it becomes cumbersome and annoying.
  3. If you find that you are more and more annoyed with, to quote you "painfully wide tables and "type" columns" then:
    • Your table design needs to be rethought and redone
    • Your models may not be a good candidates for STI
    • You just have to live with it.

Most IT problems really come down to this: Effort vs Benefit.

In your case you should ask yourself this question:

  • How much time do you want to spend on implementing a better STI structure if it will only speed up your raw SQL query by a few seconds? Maybe it's better to write a more explicative SQL query? Most applications don't grow to the size where it really becomes an issue. But it maybe different in your case.

P.S.:

Also a quick tip on structuring STI in your app: If you find that you have a lot of models that use STI like a ProductCategory, CommentCategory, PhoneCategory, ClientCategory that all inherit from Cateogory - I tend to organize them in folders inside model directory. Then in application.rb just add a line: config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**/**}')]

3
  • 5
    When have you ever in the course of your career switch production database engines? :P I'm talking about big-boy companies, not your "start up" that you started with your HS friends and you're the CEO and she's the CTO.
    – Volte
    Commented Jul 21, 2015 at 20:43
  • Sort of true, and sort of not. Yes normally you don't switch production implementation that much , unless you have to. Twitter & Facbook being the prime examples that spring to mind. Thou migrated to new setup over a course of some time. From a personal experience thou - it happened to me several times. I had to migrate internal apps from old obsolete setups.
    – konung
    Commented Sep 9, 2015 at 15:18
  • @bwicklund - well the question was about PostgreSQL & STI in ActiveRecord ( Rails) and if there a NICE CLEAN way to handle it - and the answer was that there were NO nice clean way to handle STI except in simple cases, and I explained why. Just cause you don't like the answer it doesn't mean it wrong. If you have a better explanation feel free to provide your own answer.
    – konung
    Commented Jan 29, 2016 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.