create table users (
	id serial primary key,
	login varchar(20),
	passwd varchar(20),
	email varchar(50)
);
grant all on users to nobody;
grant all on users_id_seq to nobody;

create table items (
	id serial primary key,    -- THIS IS THE TOP PRIMARY KEY IN QUESTION
	-- "what" field says what this is:
	--     'c' = city
	--     'h' = hotel.  etc, finish list
	what char not null,
	added_by int4 not null references users,
	added_date date not null default now()::date,
	edited_by int4 references users,
	edited_date date,
	name varchar(30) not null
);
grant all on items to nobody;
grant all on items_id_seq to nobody;

create table major_locations (
	-- for things that would be on a country map, including cities
	-- and other points of interest not in cities/towns
	longitude real,
	latitude real,
	elevation int2,   -- stored in meters
	country char(2) not null,
	locality varchar(30)   -- that's for state or province name
) inherits (items);
grant all on major_locations to nobody;

create table cities (
	population int4
) inherits (major_locations);
grant all on cities to nobody;

create table info (
	id serial primary key,
	what int4 not null references items,  -- THIS IS THE FOREIGN KEY IN QUESTION
	uid int4 not null references users,
	gold bool not null default 'f' not null,
	ts datetime not null default now(),
	ikey varchar(50) not null,
	msg varchar(12000)
);
grant all on info to nobody;
grant all on info_id_seq to nobody;

 otg=# select * from users;
 id | login | passwd |      email
----+-------+--------+------------------
  1 | micah | XXXX   | yodermk@home.com
(1 row)

otg=# select * from items;
 id | what | added_by | added_date | edited_by | edited_date | name
----+------+----------+------------+-----------+-------------+-------
  1 | c    |        1 | 2001-06-23 |           |             | Salem
(1 row)

otg=# select * from cities;
 id | what | added_by | added_date | edited_by | edited_date | name  | longitude | latitude | elevation | country | locality | population
----+------+----------+------------+-----------+-------------+-------+-----------+----------+-----------+---------+----------+------------
  1 | c    |        1 | 2001-06-23 |           |             | Salem |      -123 |       45 |        30 | us      | or       |     120000
(1 row)

HERE IT IS.  This should work, as the 'what' column of info (referencing the 'items' table)
is getting a 1, which is a city, a descendent of an item.

otg=# insert into info (what, uid, ikey, msg) values (1, 1, 'General Info', 'Salem is the capital of Oregon!');
ERROR:  <unnamed> referential integrity violation - key referenced from info not found in items

But we'll put in another row in the base class 'items', and an insert into the 'info' table with what=2
(the new item ID which is only a member of the top class) will succeed.

otg=# insert into items (what, added_by, name) values ('x', 1, 'Test');
INSERT 20129 1
otg=# select * from items;
 id | what | added_by | added_date | edited_by | edited_date | name
----+------+----------+------------+-----------+-------------+-------
  2 | x    |        1 | 2001-06-23 |           |             | Test
  1 | c    |        1 | 2001-06-23 |           |             | Salem
(2 rows)

otg=# select * from cities;
 id | what | added_by | added_date | edited_by | edited_date | name  | longitude | latitude | elevation | country | locality | population
----+------+----------+------------+-----------+-------------+-------+-----------+----------+-----------+---------+----------+------------
  1 | c    |        1 | 2001-06-23 |           |             | Salem |      -123 |       45 |        30 | us      | or       |     120000
(1 row)

otg=# insert into info (what, uid, ikey, msg) values (1, 1, 'General Info', 'Salem is the capital of Oregon!');
ERROR:  <unnamed> referential integrity violation - key referenced from info not found in items
otg=# insert into info (what, uid, ikey, msg) values (2, 1, 'General Info', 'Salem is the capital of Oregon!');
INSERT 20131 1

 otg=# select version();
                           version
-------------------------------------------------------------
 PostgreSQL 7.1.2 on i686-pc-linux-gnu, compiled by GCC 2.96
(1 row)
