Обсуждение: Inheritance Question

Поиск
Список
Период
Сортировка

Inheritance Question

От
"Neil Burrows"
Дата:
Hi,

A quick question about Inheritance here.

If a table has FOREIGN or PRIMARY KEYs in it, and then another table which
inherits this table is created, the inherited table will not have the KEY
constraints.  Is this correct?

In the documentation it is does not explicitly say whether constraints are
inherited or not so I just wanted to check (it might also be worth adding it
to the docs for dumb people like me :-)

I've included an example below in case my explanation is not clear.

This is actually the way I want it to work for keeping a history, but I'm
just checking that this is the correct behaviour and it's not going to be
"fixed" in a future version (which would mean I would have to rewrite the
scripts).

Thanks in advance,

---[  Neil Burrows  ]-----------------------------------------------------
E-mail: neil.burrows@ssh.com      |   SSH Communication Security Corp.
Web   : http://www.ssh.com/       |     Keeping the Internet secure
-----< Any views expressed are not necessarily those of my employer >-----




CREATE TABLE ref(
id    INT4 PRIMARY KEY,
val   VARCHAR(128)
);

CREATE TABLE parent(
id    INT4 PRIMARY KEY,
ref   INT4,
val   VARCHAR(128),
FOREIGN KEY (ref) REFERENCES ref(id)
);

CREATE TABLE parent_history(
) inherits(parent);

CREATE FUNCTION backup_parent () RETURNS OPAQUE as '
BEGIN
  INSERT INTO parent_history SELECT * FROM parent WHERE id = OLD.id;
  return OLD;
END; '
LANGUAGE 'plpgsql';

CREATE TRIGGER parent_trigger
BEFORE DELETE OR UPDATE ON parent FOR EACH ROW
EXECUTE PROCEDURE backup_parent ();


INSERT INTO ref VALUES (1, 'test ref');
INSERT INTO parent VALUES (1, 1, 'test parent');

-- This should fail to to integrety check
DELETE FROM ref;

DELETE FROM parent;

-- This will no longer fail
DELETE FROM ref;

-- Show value in history
SELECT * FROM parent_history;


Re: Inheritance Question

От
Stephan Szabo
Дата:
On Wed, 21 Feb 2001, Neil Burrows wrote:

> Hi,
>
> A quick question about Inheritance here.
>
> If a table has FOREIGN or PRIMARY KEYs in it, and then another table which
> inherits this table is created, the inherited table will not have the KEY
> constraints.  Is this correct?

Yes currently, although that's not guaranteed to stay true, since most
people using inheritance want the constraints to be inherited, there's
just been a question of what that means precisely and how to do it.

For what you're doing, I'd suggest making a base table that defines the
"type" and inheriting two copies one for your main table and one for the
history.