473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Issue adding foreign key

I have 2 existing tables in my db:

iss=> \d pollgrpinfo
Table "public.pollgrpinfo"
Column | Type | Modifiers
---------------+------------------------+-----------
pollgrpinfoid | integer | not null
pollgrpid | integer | not null
name | character varying(100) |
descript | character varying(200) |
Indexes:
"pollgrpinfo_pkey" primary key, btree (pollgrpinfoid)
"pollgrpinfo_pollgrpid_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_pkey" primary key, btree (notpollid)
"notpoll_pollgrpid_key" unique, btree (pollgrpid)
"notpoll_alerting_index" btree (alerting)
Triggers:
"RI_ConstraintTrigger_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)=(7685) is not present in table "pollgrpinfo".

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
www.iglass.net

---------------------------(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

Nov 23 '05 #1
1 3687
ge*************@iglass.net ("George Woodring") writes:
I have 2 existing tables in my db:

iss=> \d pollgrpinfo
Table "public.pollgrpinfo"
Column | Type | Modifiers
---------------+------------------------+-----------
pollgrpinfoid | integer | not null
pollgrpid | integer | not null
name | character varying(100) |
descript | character varying(200) |
Indexes:
"pollgrpinfo_pkey" primary key, btree (pollgrpinfoid)
"pollgrpinfo_pollgrpid_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_pkey" primary key, btree (notpollid)
"notpoll_pollgrpid_key" unique, btree (pollgrpid)
"notpoll_alerting_index" btree (alerting)
Triggers:
"RI_ConstraintTrigger_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)=(7685) is not present in table "pollgrpinfo".


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
Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ab | last post by:
hi all, we need to ALTER TABLE to add a foreign key, but we cannot find the correct syntax. TAG TAGNUMBER, integer (primary key) TAG, char(16) TAGDESCRIPTION, char(32)
4
by: Jean-Christian Imbeault | last post by:
Currently I have a table defined as such: TAL=# \d internal_keywords Table "public.internal_keywords" Column | Type | Modifiers ---------+---------+----------- keyword | text | not null...
1
by: Darius | last post by:
I was having an issue inserting data into a table in an I-Series DB2 database. The Insert statement itself is very simple: Insert into Table1 select * from Table2 These two tables have...
3
by: Raj | last post by:
Hi, I am trying to add some more information to the table which already has a lot a data (like 2-3000 records). The new information may be adding 2-3 new columns worth. Now my questions are:...
20
by: Bryan | last post by:
hello all... im trying to add a record to an sql db on ms sql server 2000, using vb.net. seems to be working.. except for one thing, one of the columns in the database is a bit datatype, and...
12
by: mike | last post by:
I have set up a FK as follows ALTER TABLE lk_sub_con ADD FOREIGN KEY (type) REFERENCES lk_sort_of_contact(type_code); However when I do this INSERT INTO lk_sort_of_contact (type_code)...
2
by: headache | last post by:
Greetings. I'm a first time poster, so feel free to clue me in to any protocol I may have inadvertently violated. I have an issue that has arisen where I need to join 3 tables where 2 of the 3...
0
by: ckiraly | last post by:
Greetings everyone - I am new to MSSQL 2005, and have started a database design project for my company. The issue I have is in a specific instance of foreign key creation. Here is the whole...
3
by: Shestine | last post by:
I am trying to add a column to a current table, with data in it. I am only learning, and i have no idea how to change this to make it work. Here is the script I have right now it, but what it does is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.