Re: How hard would a "path" operator be to implement in PostgreSQL

From: Chris Travers <chris(dot)travers(at)gmail(dot)com>
To: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: How hard would a "path" operator be to implement in PostgreSQL
Date: 2012-08-22 10:00:40
Message-ID: CAKt_ZftXBajnn26xGt261zgJU5xXo6D_Ve1QgpApSHDS-94sGw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all;

So I found an interesting and relatively manageable way of doing this.

Suppose we have an inventory table:

CREATE TABLE inventory_item (
id serial primary key,
cogs_account_id int references account(id),
inv_account_id int references account(id),
income_account_id int references account(id),
sku text not null,
description text,
last_cost numeric, -- null if never purchased
sell_price numeric not null,
active bool not null default true
);

Now we want to be able to add pointers to this table in other tables
without adding a lot of decentralized code. So what we do is:

CREATE TABLE joins_inventory_item (
inventory_item_id int
);

Then we create a table method function like:

CREATE FUNCTION inventory_item(joins_inventory_item) RETURNS inventory_item
LANGUAGE SQL AS $$
SELECT * FROM inventory_item where id = $1.inventory_item_id;
$$;

Then any table which inherits joins_inventory_item gets a path back.
So for example:

CREATE TABLE inventory_barcode (
barcode text primary key;
FOREIGN KEY inventory_item_id REFERENCES inventory_item(id)
);

Then we can:

select (bc.inventory_item).sku FROM inventory_barcode bc WHERE barcode
= '12345';

Best Wishes,
Chris Travers

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Seref Arikan 2012-08-22 10:03:37 Re: Are there any options to parallelize queries?
Previous Message hubert depesz lubaczewski 2012-08-22 09:00:36 Re: Problems with timestamp with time zone and old dates?