pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
simple addition of constraints problem
3+ messages / 3 participants
[nested] [flat]

* simple addition of constraints problem
@ 2004-06-08 21:51 Michelle Murrain <tech@murrain.net>
  2004-06-08 22:55 ` Re: [SQL] simple addition of constraints problem Richard Poole <rp@guests.deus.net>
  2004-06-08 23:18 ` Re: simple addition of constraints problem Stephan Szabo <sszabo@megazone.bigpanda.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Michelle Murrain @ 2004-06-08 21:51 UTC (permalink / raw)
  To: pgsql-sql

Hi All,

I'm trying to do something which seems really simple to me. (Postgres 7.3.4)

I've got this table:

charter_dev2=# \d rcourseinfo
                                Table "public.rcourseinfo"
    Column    |         Type          |                     Modifiers
-------------+-----------------------+---------------------------------------------------
  courseid    | integer               | not null default 
nextval('rcourseinfo_seq'::text)
  coursename  | character varying(50) |
  dept        | character varying(30) |
  number      | character varying(4)  |
  section     | character varying(2)  |
  trimester   | character varying(5)  |
  schoolyear  | character varying(8)  |
  facultyid   | integer               |
  description | text                  |
  credits     | real                  |
Indexes: rcourseinfo_pkey primary key btree (courseid),
          rcourseinfo_number_index btree (number)

With a primary key called 'courseid'.

I've got a second table:

charter_dev2=# \d coursesevaluations
                              Table "public.coursesevaluations"
         Column         |  Type   |                        Modifiers
-----------------------+---------+----------------------------------------------------------
  courseid              | integer |
  evalid                | integer |
  coursesevaluations_id | integer | not null default 
nextval('coursesevaluations_seq'::text)
Indexes: coursesevaluations_pkey primary key btree (coursesevaluations_id),
          coursesevaluations_evalid_index btree (evalid)


I'd like to make 'courseid' in this second table a foreign key, 
referencing 'courseid' in the first table. So I did this command:

charter_dev2=# ALTER TABLE coursesevaluations ADD CONSTRAINT rc_ce 
FOREIGN KEY (courseid) REFERENCES rcourseinfo (courseid);

And I get this result:

NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
ERROR:  rc_ce referential integrity violation - key referenced from 
coursesevaluations not found in rcourseinfo

This doesn't make any sense to me - the fields are named the same, 
and are the same data type. I've pored over the docs, to no avail. 
I'm sure I'm missing something really elementary, but it's escaping 
me.

I did try the following (because the error said "key referenced"):
ALTER TABLE coursesevaluations ADD CONSTRAINT rc_ce FOREIGN KEY 
(courseid) REFERENCES rcourseinfo (rcourseinfo_pkey);

I got the error :
ERROR:  ALTER TABLE: column "rcourseinfo_pkey" referenced in foreign 
key constraint does not exist

!!

Thanks for any advice.
-- 
.Michelle

--------------------------
Michelle Murrain
mmurrain at dbdes dot com
413-222-6350 ph
617-889-0929 ph
952-674-7253 fax <--- new
Page: pageme@murrain.net
AIM:pearlbear0 ICQ:129250575
Y!: michelle_murrain Jabber: pearlbear@transactim.net

"Work like you don't need the money. Love like you've never been 
hurt. Dance like nobody's watching." - Satchel Paige



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: [SQL] simple addition of constraints problem
  2004-06-08 21:51 simple addition of constraints problem Michelle Murrain <tech@murrain.net>
@ 2004-06-08 22:55 ` Richard Poole <rp@guests.deus.net>
  1 sibling, 0 replies; 3+ messages in thread

From: Richard Poole @ 2004-06-08 22:55 UTC (permalink / raw)
  To: pgsql-general@postgresql.org

On Tue, Jun 08, 2004 at 05:51:22PM -0400, Michelle Murrain wrote:

> I'd like to make 'courseid' in this second table a foreign key, 
> referencing 'courseid' in the first table. So I did this command:
> 
> charter_dev2=# ALTER TABLE coursesevaluations ADD CONSTRAINT rc_ce 
> FOREIGN KEY (courseid) REFERENCES rcourseinfo (courseid);
> 
> And I get this result:
> 
> NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY 
> check(s)
> ERROR:  rc_ce referential integrity violation - key referenced from 
> coursesevaluations not found in rcourseinfo

What this is telling you is not that there is any problem with your
formulation of the constraint (which as far as I can tell is what you
want), but that the existing data in the tables violates the constraint
you're trying to impose. If you do something like

SELECT rcoursesevaluations.courseid
FROM rcoursesevalutations NATURAL LEFT JOIN rcourseinfo
WHERE rcourseinfo.courseid IS NULL

you should see which courseids appear in rcoursesevaluations but not in
rcourseinfo.


Richard



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: simple addition of constraints problem
  2004-06-08 21:51 simple addition of constraints problem Michelle Murrain <tech@murrain.net>
@ 2004-06-08 23:18 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Stephan Szabo @ 2004-06-08 23:18 UTC (permalink / raw)
  To: Michelle Murrain <tech@murrain.net>; +Cc: pgsql-sql


On Tue, 8 Jun 2004, Michelle Murrain wrote:

> I'm trying to do something which seems really simple to me. (Postgres 7.3.4)
>
> I've got this table:
>
> charter_dev2=# \d rcourseinfo
>                                 Table "public.rcourseinfo"
>     Column    |         Type          |                     Modifiers
> -------------+-----------------------+---------------------------------------------------
>   courseid    | integer               | not null default
> nextval('rcourseinfo_seq'::text)
>   coursename  | character varying(50) |
>   dept        | character varying(30) |
>   number      | character varying(4)  |
>   section     | character varying(2)  |
>   trimester   | character varying(5)  |
>   schoolyear  | character varying(8)  |
>   facultyid   | integer               |
>   description | text                  |
>   credits     | real                  |
> Indexes: rcourseinfo_pkey primary key btree (courseid),
>           rcourseinfo_number_index btree (number)
>
> With a primary key called 'courseid'.
>
> I've got a second table:
>
> charter_dev2=# \d coursesevaluations
>                               Table "public.coursesevaluations"
>          Column         |  Type   |                        Modifiers
> -----------------------+---------+----------------------------------------------------------
>   courseid              | integer |
>   evalid                | integer |
>   coursesevaluations_id | integer | not null default
> nextval('coursesevaluations_seq'::text)
> Indexes: coursesevaluations_pkey primary key btree (coursesevaluations_id),
>           coursesevaluations_evalid_index btree (evalid)
>
>
> I'd like to make 'courseid' in this second table a foreign key,
> referencing 'courseid' in the first table. So I did this command:
>
> charter_dev2=# ALTER TABLE coursesevaluations ADD CONSTRAINT rc_ce
> FOREIGN KEY (courseid) REFERENCES rcourseinfo (courseid);
>
> And I get this result:
>
> NOTICE:  ALTER TABLE will create implicit trigger(s) for FOREIGN KEY check(s)
> ERROR:  rc_ce referential integrity violation - key referenced from
> coursesevaluations not found in rcourseinfo
>
> This doesn't make any sense to me - the fields are named the same,
> and are the same data type. I've pored over the docs, to no avail.
> I'm sure I'm missing something really elementary, but it's escaping
> me.

That's the message for the constraint violation.  It looks like perhaps
the data doesn't meet the constraint (in 7.4 it would have shown you
values, for 7.3 you'll have to look yourself).




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-06-08 23:18 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-06-08 21:51 simple addition of constraints problem Michelle Murrain <tech@murrain.net>
2004-06-08 22:55 ` Richard Poole <rp@guests.deus.net>
2004-06-08 23:18 ` Stephan Szabo <sszabo@megazone.bigpanda.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox