Thread: relation vs table...

relation vs table...

From
Terrence Brannon
Date:
I don't know what Postgres considers a relation and had no intention of
creating one when piping my schema to it... I always DROP TABLE before
CREATE TABLE, so here are the ERRORS emitted when building the database:

    3:ERROR:  table "country" does not exist
    6:ERROR:  table "customer" does not exist
   11:ERROR:  table "product" does not exist
   15:ERROR:  table "license" does not exist
   19:ERROR:  table "enduser" does not exist
   24:ERROR:  table "orders" does not exist
   29:ERROR:  table "enduser_license" does not exist
   33:ERROR:  table "item" does not exist
   37:ERROR:  Relation "product_version" does not exist
   38:ERROR:  table "product_version" does not exist
   43:ERROR:  table "ordered_item" does not exist

but each and every one of these was created via CREATE TABLE

I don't understand why Postgres thinks I am creating a relation _and_ I
don't know what it considers a relation to be. Create stmts follow:



/*==========================================================================*/
/*
Tables                                                                  */
/*==========================================================================*/

DROP TABLE country;
CREATE TABLE country (
    country_id VARCHAR(3) PRIMARY KEY,

    country VARCHAR(80)
);

DROP TABLE customer;
CREATE TABLE    customer (
    customer_id SERIAL PRIMARY KEY,
    country_id VARCHAR(3) REFERENCES country(country_id),

    name VARCHAR(100) NOT NULL,
    companyname VARCHAR(100) NOT NULL,
    address1 VARCHAR(100) NOT NULL,
    address2 VARCHAR(100) NOT NULL,
    city VARCHAR(100) NOT NULL,
    zipcode VARCHAR(10) NOT NULL,
    state VARCHAR(10) NOT NULL,

    phone VARCHAR(20),
    fax VARCHAR(20),
    email VARCHAR(100),
    vatid VARCHAR(20)
);

DROP TABLE product;
CREATE TABLE    product (
    product_id  SERIAL PRIMARY KEY,

    productdesc VARCHAR(100) NOT NULL,
    productname VARCHAR(100) NOT NULL
);

DROP TABLE license;
CREATE TABLE    license (
    license_id  SERIAL PRIMARY KEY,

    lickey VARCHAR(80) NOT NULL,
    liccode VARCHAR(80) NOT NULL
);

DROP TABLE enduser;
CREATE TABLE    enduser (
    enduser_id  SERIAL PRIMARY KEY,
    customer_id INTEGER REFERENCES customer(customer_id),

    name VARCHAR(40),
    companyname VARCHAR(40)
);

DROP TABLE orders;
CREATE TABLE orders (
    orders_id SERIAL PRIMARY KEY,
    country_id VARCHAR(3) REFERENCES country(country_id),
    customer_id INTEGER REFERENCES customer(customer_id),

    name VARCHAR(100) NOT NULL,
    companyname VARCHAR(100) NOT NULL,
    address1 VARCHAR(100) NOT NULL,
    address2 VARCHAR(100) NOT NULL,
    city VARCHAR(100) NOT NULL,
    state VARCHAR(10) NOT NULL,
    zipcode VARCHAR(10) NOT NULL,

    orderdate DATE,
    phone VARCHAR(20),
    fax VARCHAR(20),
    email VARCHAR(100),
    vatid VARCHAR(20),
    refno VARCHAR(100),
    promotion VARCHAR(100)
);

DROP TABLE enduser_license;
CREATE TABLE enduser_license (
    license_id INTEGER REFERENCES license(license_id),
    enduser_id INTEGER REFERENCES enduser(enduser_id),

    PRIMARY KEY (license_id, enduser_id)
);

DROP TABLE item;
CREATE TABLE item (
    item_id SERIAL,
    product_version_id INTEGER REFERENCES
product_version(product_version_id),

    active INTEGER NOT NULL,
    itemname VARCHAR(100) NOT NULL,
    unitprice DECIMAL,
    numberoflic INTEGER,

    PRIMARY KEY (item_id, product_version_id)
);

DROP TABLE product_version;
CREATE TABLE product_version (
    product_version_id SERIAL PRIMARY KEY,
    product_id INTEGER REFERENCES product(product_id)
);

DROP TABLE ordered_item;
CREATE TABLE ordered_item (
    orders_id INTEGER REFERENCES orders(orders_id),
    item_id INTEGER NOT NULL,
    product_id INTEGER NOT NULL,

    quantity INTEGER NOT NULL,
    product_version_id VARCHAR(10),
    unitprice DECIMAL,

    PRIMARY KEY (orders_id, item_id, product_id)
);


/*==========================================================================*/
/*
Indexes                                                                 */
/*==========================================================================*/


/*==========================================================================*/
/*
Procedures                                                              */
/*==========================================================================*/

/*==========================================================================*/
/*
Triggers                                                                */
/*==========================================================================*/





Re: relation vs table...

From
Alvaro Herrera
Date:
On Thu, Oct 09, 2003 at 05:50:48AM -0700, Terrence Brannon wrote:
> I don't know what Postgres considers a relation and had no intention of
> creating one when piping my schema to it... I always DROP TABLE before
> CREATE TABLE, so here are the ERRORS emitted when building the database:

Well, when you do a DROP TABLE and there's no table with the name you
give, the system is right in giving you a "table foo does not exist".
However, when you create a foreign key to a table that doesn't exist
(because you are creating it further down in your script), the system
doesn't know it is a table (it could be a view, for example).  So it
falls back to using the more general term, "relation".

"Relation" means, in Postgres terms, "anything that can have a pg_class
entry".  This means system tables and views, regular tables and views,
indexes, sequences, TOAST tables and special relations like pg_xactlock
if I'm not mistaken.

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Estoy de acuerdo contigo en que la verdad absoluta no existe...
El problema es que la mentira sí existe y tu estás mintiendo" (G. Lama)

Re: relation vs table...

From
Peter Eisentraut
Date:
Terrence Brannon writes:

> I don't understand why Postgres thinks I am creating a relation _and_ I
> don't know what it considers a relation to be.

In the context of relational databases, relation means the same thing as
table.  Because of some implementation artifacts, PostgreSQL internally
treats tables, views, indexes, and sequences alike to some extent and
refers to all of them together as relations.  So when you see an error
message telling you that a relation was not found, that means PostgreSQL
was looking for a table, a view, an index, or a sequence.  This artificial
terminology isn't ideal, but it creates few problems in practice.

--
Peter Eisentraut   peter_e@gmx.net


Re: relation vs table...

From
Oliver Elphick
Date:
On Thu, 2003-10-09 at 13:50, Terrence Brannon wrote:
> I don't know what Postgres considers a relation and had no intention of
> creating one when piping my schema to it... I always DROP TABLE before
> CREATE TABLE, so here are the ERRORS emitted when building the database:
>
>     3:ERROR:  table "country" does not exist
>     6:ERROR:  table "customer" does not exist
>    11:ERROR:  table "product" does not exist
>    15:ERROR:  table "license" does not exist
>    19:ERROR:  table "enduser" does not exist
>    24:ERROR:  table "orders" does not exist
>    29:ERROR:  table "enduser_license" does not exist
>    33:ERROR:  table "item" does not exist

All these are errors when you DROP a table that does not yet exist.

>    37:ERROR:  Relation "product_version" does not exist

This is your attempt to have a foreign key reference to product_version
from table "item", when product_version does not yet exist.

>    38:ERROR:  table "product_version" does not exist
>    43:ERROR:  table "ordered_item" does not exist

and again, you are dropping these tables before they exist.

> but each and every one of these was created via CREATE TABLE

but only _after_ you try to drop them (and your attempt to create "item"
will have failed because of the bad foreign key reference).

> I don't understand why Postgres thinks I am creating a relation _and_ I
> don't know what it considers a relation to be. Create stmts follow:

A table is a relation but a relation is not necessarily a table,  A view
is a relation.  However, the error message for your bad foreign key is
misleading because a foreign key must reference a table, not a relation:

junk=# create view aaa as select * from xxx union select * from zzz;
CREATE VIEW
junk=# create table bbb (id serial, aid integer references aaa(id));
NOTICE:  CREATE TABLE will create implicit sequence "bbb_id_seq" for
"serial" column "bbb.id"
ERROR:  referenced relation "aaa" is not a table

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "Every good gift and every perfect gift is from above,
      and cometh down from the Father of lights, with whom
      is no variableness, neither shadow of turning."
                                   James 1:17