Issue adding foreign key

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George Woodring

    #1

    Issue adding foreign key

    I have 2 existing tables in my db:

    iss=> \d pollgrpinfo
    Table "public.pollgrp info"
    Column | Type | Modifiers
    ---------------+------------------------+-----------
    pollgrpinfoid | integer | not null
    pollgrpid | integer | not null
    name | character varying(100) |
    descript | character varying(200) |
    Indexes:
    "pollgrpinfo_pk ey" primary key, btree (pollgrpinfoid)
    "pollgrpinfo_po llgrpid_key" unique, btree (pollgrpid)

    iss=> \d notpoll
    Table "public.notpoll "
    Column | Type | Modifiers
    -------------+------------------------+---------------------
    notpollid | integer | not null
    pollgrpid | integer |
    notgroupsid | integer |
    alerting | character(1) | default 'y'::bpchar
    disuser | character varying(50) |
    distime | integer |
    alertingcom | character varying(200) |
    Indexes:
    "notpoll_pk ey" primary key, btree (notpollid)
    "notpoll_pollgr pid_key" unique, btree (pollgrpid)
    "notpoll_alerti ng_index" btree (alerting)
    Triggers:
    "RI_ConstraintT rigger_2110326" AFTER INSERT OR UPDATE ON notpoll
    FROM notgroups NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE
    PROCEDURE "RI_FKey_check_ ins"('notgroups _exists', 'notpoll',
    'notgroups', 'UNSPECIFIED', 'notgroupsid', 'notgroupsid')

    I am trying to add a foreign key to the notpoll table

    iss=> alter table notpoll add constraint pollgrp_exists foreign
    key(pollgrpid) references pollgrpinfo on delete cascade;
    ERROR: insert or update on table "notpoll" violates foreign key
    constraint "pollgrp_exists "
    DETAIL: Key (pollgrpid)=(76 85) is not present in table "pollgrpinf o".

    I have verified that the information is in the pollgrpinfo table:

    iss=> select * from pollgrpinfo where pollgrpid=7685;
    pollgrpinfoid | pollgrpid | name | descript
    ---------------+-----------+------+----------
    767 | 7685 | HTTP |
    (1 row)


    I could use a suggestion on how to proceed in troubleshooting the error
    message. I am running 7.4.5

    Thanks,
    Woody

    ----------------------------------------
    iGLASS Networks
    211-A S. Salem St
    Apex NC 27502
    (919) 387-3550 x813
    iGLASS offers 24x7 NOC services for IT monitoring & outage resolution. Let us enhance your network reliability. Contact us for a quote!


    ---------------------------(end of broadcast)---------------------------
    TIP 9: the planner will ignore your desire to choose an index scan if your
    joining column's datatypes do not match

  • Edmund Bacon

    #2
    Re: Issue adding foreign key

    george.woodring @iglass.net ("George Woodring") writes:
    [color=blue]
    > I have 2 existing tables in my db:
    >
    > iss=> \d pollgrpinfo
    > Table "public.pollgrp info"
    > Column | Type | Modifiers
    > ---------------+------------------------+-----------
    > pollgrpinfoid | integer | not null
    > pollgrpid | integer | not null
    > name | character varying(100) |
    > descript | character varying(200) |
    > Indexes:
    > "pollgrpinfo_pk ey" primary key, btree (pollgrpinfoid)
    > "pollgrpinfo_po llgrpid_key" unique, btree (pollgrpid)
    >
    > iss=> \d notpoll
    > Table "public.notpoll "
    > Column | Type | Modifiers
    > -------------+------------------------+---------------------
    > notpollid | integer | not null
    > pollgrpid | integer |
    > notgroupsid | integer |
    > alerting | character(1) | default 'y'::bpchar
    > disuser | character varying(50) |
    > distime | integer |
    > alertingcom | character varying(200) |
    > Indexes:
    > "notpoll_pk ey" primary key, btree (notpollid)
    > "notpoll_pollgr pid_key" unique, btree (pollgrpid)
    > "notpoll_alerti ng_index" btree (alerting)
    > Triggers:
    > "RI_ConstraintT rigger_2110326" AFTER INSERT OR UPDATE ON notpoll
    > FROM notgroups NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE
    > PROCEDURE "RI_FKey_check_ ins"('notgroups _exists', 'notpoll',
    > 'notgroups', 'UNSPECIFIED', 'notgroupsid', 'notgroupsid')
    >
    > I am trying to add a foreign key to the notpoll table
    >
    > iss=> alter table notpoll add constraint pollgrp_exists foreign
    > key(pollgrpid) references pollgrpinfo on delete cascade;
    > ERROR: insert or update on table "notpoll" violates foreign key
    > constraint "pollgrp_exists "
    > DETAIL: Key (pollgrpid)=(76 85) is not present in table "pollgrpinf o".
    >[/color]

    When expressed as "FOREIGN KEY (foo) REFERENCES mytable" postgresql
    assumes that foo references the PRIMARY KEY for table mytable. If you
    are not referencing the primary key, you need to tell postgresql which
    column e.g. FOREIGN KEY (foo) REFERNCES mytable(foo).


    --
    Remove -42 for email

    Comment

    Working...