Thread: Problems w. SERIAL

Problems w. SERIAL

From
Morten Primdahl
Date:
Hi.

Have the following tables:

CREATE TABLE tbl_a (id SERIAL PRIMARY KEY, data VARCHAR(5));
CREATE TABLE tbl_b (id SERIAL PRIMARY KEY, data VARCHAR(5));

CREATE TABLE tbl_c
 (id SERIAL PRIMARY KEY,
  data VARCHAR(50),
  a SERIAL CONSTRAINT a_ref REFERENCES tbl_a(id),
  b SERIAL CONSTRAINT b_ref REFERENCES tbl_b(id)
);

The two inserts into tbl_a and tbl_b work fine:

INSERT INTO tbl_a (data) VALUES ('a data');
INSERT INTO tbl_b (data) VALUES ('b data');

But if I do

INSERT INTO tbl_c (data, a, b) VALUES ('c data',1,1);

I get ERROR:  Relation 'tbl_c_id_seq' does not exist
even though that sequence was implicitly created upon
creation of tbl_c - or?

Can anyone help me out on this? Thanks.

Morten

Re: Problems w. SERIAL

From
missive@frontiernet.net (Lee Harr)
Date:
On Sat, 05 May 2001 19:34:06 +0200, Morten Primdahl <morten@primdahl.net> wrote:
>Hi.
>
>Have the following tables:
>
>CREATE TABLE tbl_a (id SERIAL PRIMARY KEY, data VARCHAR(5));
>CREATE TABLE tbl_b (id SERIAL PRIMARY KEY, data VARCHAR(5));
>
>CREATE TABLE tbl_c
> (id SERIAL PRIMARY KEY,
>  data VARCHAR(50),
>  a SERIAL CONSTRAINT a_ref REFERENCES tbl_a(id),
>  b SERIAL CONSTRAINT b_ref REFERENCES tbl_b(id)
>);
>
>The two inserts into tbl_a and tbl_b work fine:
>
>INSERT INTO tbl_a (data) VALUES ('a data');
>INSERT INTO tbl_b (data) VALUES ('b data');
>
>But if I do
>
>INSERT INTO tbl_c (data, a, b) VALUES ('c data',1,1);
>
>I get ERROR:  Relation 'tbl_c_id_seq' does not exist
>even though that sequence was implicitly created upon
>creation of tbl_c - or?
>
>Can anyone help me out on this? Thanks.
>
>Morten


Can you do

SELECT nextval( 'tbl_c_id_seq' );

in psql successfully?
if not, is the sequence actually there? try

\d

or

\d tbl_c_id_seq


if it is not there, you can create it yourself

CREATE SEQUENCE tbl_c_id_seq;


if it is there... not sure what to say.


Re: Problems w. SERIAL

From
Tom Lane
Date:
Morten Primdahl <morten@primdahl.net> writes:
> I get ERROR:  Relation 'tbl_c_id_seq' does not exist
> even though that sequence was implicitly created upon
> creation of tbl_c - or?

Indeed, it should have been --- and is when I repeat the test
commands you give.  Perhaps you mistakenly removed the sequence
later on?

            regards, tom lane

Re: Problems w. SERIAL

From
Joel Burton
Date:
On Sat, 5 May 2001, Morten Primdahl wrote:

> CREATE TABLE tbl_a (id SERIAL PRIMARY KEY, data VARCHAR(5));
> CREATE TABLE tbl_b (id SERIAL PRIMARY KEY, data VARCHAR(5));
>
> CREATE TABLE tbl_c
>  (id SERIAL PRIMARY KEY,
>   data VARCHAR(50),
>   a SERIAL CONSTRAINT a_ref REFERENCES tbl_a(id),
>   b SERIAL CONSTRAINT b_ref REFERENCES tbl_b(id)
> );
>
> INSERT INTO tbl_c (data, a, b) VALUES ('c data',1,1);
>
> I get ERROR:  Relation 'tbl_c_id_seq' does not exist
> even though that sequence was implicitly created upon
> creation of tbl_c - or?

re: structure of table C, I think you're misunderstand something
about SERIALs. A SERIAL is just another name for an int that automatically
has a sequence set up for it. So, if in table C, you want to store
references to tables a and b, you want you schema to be:

CREATE TABLE tbl_c (
  id    serial primary key,
  a     int references tbl_a,
  b     int references tbl_b
);

note the use of INTs, not SERIALs.

I don't know if that will explain the weirdness of your error message,
but, even if not, it will certainly a future error! :-)

HTH,
--
Joel Burton <jburton@scw.org>
Director of Information Systems, Support Center of Washington


Re: Problems w. SERIAL

From
Tom Lane
Date:
Morten Primdahl <morten@it-c.dk> writes:
> I certain that I do not remove those. It happens every time
> I run the aforementioned sequence. This is postgresql 7.0.3,
> I'll upgrade to 7.1 and try again. Thanks.

Oh, I was trying 7.1.  Now that I think about it, I think there's
a bug in 7.0.* that causes it to forget to create all the sequences
if you define more than one SERIAL column in a table.  That might
be what you're seeing ...

            regards, tom lane

Re: Problems w. SERIAL

From
Morten Primdahl
Date:
> Morten Primdahl <morten@primdahl.net> writes:
> > I get ERROR:  Relation 'tbl_c_id_seq' does not exist
> > even though that sequence was implicitly created upon
> > creation of tbl_c - or?
>
> Indeed, it should have been --- and is when I repeat the test
> commands you give.  Perhaps you mistakenly removed the sequence
> later on?

I certain that I do not remove those. It happens every time
I run the aforementioned sequence. This is postgresql 7.0.3,
I'll upgrade to 7.1 and try again. Thanks.

Morten




Re: Problems w. SERIAL

From
Morten Primdahl
Date:
Joel Burton wrote:

> re: structure of table C, I think you're misunderstand something
> about SERIALs. A SERIAL is just another name for an int that automatically
> has a sequence set up for it. So, if in table C, you want to store
> references to tables a and b, you want you schema to be:
[snip]
> note the use of INTs, not SERIALs.

You're right, thanks! I assumed that SERIAL was a special data
type like eg. Oracle NUMBER. Using INT4 now as FK and it works like
a charm, thanks again :)

Morten