Re: A form of inheritance with PostgreSQL

From: Greg Toombs <greg(dot)toombs(at)bluebottle(dot)com>
To: Steve Midgley <public(at)misuse(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: A form of inheritance with PostgreSQL
Date: 2007-03-13 00:08:12
Message-ID: 200703130008.l2D08IGK021628@mi1.bluebottle.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hello, and thank you to Steven and everyone else that submitted input
on this issue.<br>
<br>
After reading a few more methods of doing things, I went with the
simplest one, as 1. time is of the essence, and 2. I'm stuck with
PostgreSQL 7.1 on the server I have to develop for.<br>
<br>
I set the primary key of the parent class to a serial. Children have an
integer column with constraints as the primary key and foreign key to
the parent primary key column.<br>
<br>
Thanks again,<br>
<br>
- Greg<br>
<br>
Steve Midgley wrote:
<blockquote cite="mid20070309163840(dot)66FFB9FBC7D(at)postgresql(dot)org"
type="cite">Hi Greg,
<br>
<br>
While not in a C++ framework, you might find that it's not too hard to
implement something similar in your system - It's called "Single Table
Inheritance." References to the Ruby on Rails implementation here:
<br>
<br>
<a class="moz-txt-link-freetext" href="http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance">http://wiki.rubyonrails.org/rails/pages/SingleTableInheritance</a>
<br>
<br>
It's based on Martin Fowler's Patterns of Enterprise Architecture book
- please find references to his original patterns here:
<br>
<br>
<a class="moz-txt-link-freetext" href="http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html">http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html</a>
<br>
<br>
The key, I believe, is simply adding a "type" and a "parent_id" to the
"class" table, so you can model all your types and their hierarchical
relations. Fowler's diagram is pretty clear. I think then you would
store the data in another table (or tables) and link into this
inheritance structure to establish ancestry for any piece of data (some
people try to store the data in this table too, but I think that's a
mistake personally).
<br>
<br>
If I understand what you're trying to do, you can use this design
pattern in your application language to implement an inheritance scheme
without any special database features (i.e. in a SQL-standard manner).
<br>
<br>
I hope this is helpful,
<br>
<br>
Steve
<br>
<br>
<br>
<br>
At 12:28 AM 3/9/2007, <a class="moz-txt-link-abbreviated" href="mailto:pgsql-sql-owner(at)postgresql(dot)org">pgsql-sql-owner(at)postgresql(dot)org</a> wrote:
<br>
<blockquote type="cite">Date: Thu, 08 Mar 2007 13:01:51 -0500
<br>
From: Greg Toombs <a class="moz-txt-link-rfc2396E" href="mailto:greg(dot)toombs(at)bluebottle(dot)com">&lt;greg(dot)toombs(at)bluebottle(dot)com&gt;</a>
<br>
To: <a class="moz-txt-link-abbreviated" href="mailto:pgsql-sql(at)postgresql(dot)org">pgsql-sql(at)postgresql(dot)org</a>
<br>
Subject: A form of inheritance with PostgreSQL
<br>
Message-ID: <a class="moz-txt-link-rfc2396E" href="mailto:45F04F8F(dot)8030800(at)bluebottle(dot)com">&lt;45F04F8F(dot)8030800(at)bluebottle(dot)com&gt;</a>
<br>
<br>
 Hello.
<br>
<br>
I'm trying to figure out how to nicely implement a C++ class-like
system
<br>
with PostgreSQL. Consider the following:
<br>
<br>
Tables Fruit, Apple, Orange
<br>
<br>
I want to design the foreign key scheme such that there are relations
<br>
between fruit and apple, and fruit and orange, that imply that apple is
a
<br>
fruit, and orange is a fruit.
<br>
<br>
I don't want to eliminate the existence of Apple and Orange tables,
<br>
because there will be columns specific to both Apple and Orange; if I
<br>
include these columns in Fruit, then if Fruit is an Orange, the Apple
<br>
columns will be needlessly present in Apple rows.
<br>
<br>
The different ways of implementing this scheme that I've thought of
(some
<br>
uglier than others):
<br>
<br>
- Have Fruit contain foreign keys to both Apple and Orange, and write a
<br>
check constraint in Fruit specifying that exactly one of (Apple FK,
<br>
Orange FK) needs to be non-null. The disadvantage of this method is
that
<br>
it isn't exactly loosely coupled. For every other fruit type table I
<br>
implemented I'd have to go back and add a foreign key in Fruit.
<br>
<br>
- Have a foreign key in Apple to Fruit, and in Orange to Fruit; then
<br>
somehow create a constraint that imposes uniqueness on the union of
<br>
foreign keys in both Apple and Orange. To figure out what type of fruit
a
<br>
Fruit row is, run a query for foreign keys in Orange and Apple matching
<br>
the primary key of Fruit. You'd also want to somehow create a
constraint
<br>
that the result of this query should always return exactly one row
<br>
(perhaps with a trigger?)
<br>
<br>
Any advice will be appreciated! As I'm relatively new to Postgre, I
might
<br>
need some help with the actual implementation as well.
<br>
<br>
Thank you.
<br>
<br>
- Greg
<br>
</blockquote>
<br>
<br>
---------------------------(end of
broadcast)---------------------------
<br>
TIP 6: explain analyze is your friend
<br>
</blockquote>
<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 5.0 KB

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2007-03-13 01:24:06 Re: A form of inheritance with PostgreSQL
Previous Message Jorge Godoy 2007-03-12 20:50:58 Re: Installing with libs of postgresql-libs