pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ashish Karalkar <ashish.karalkar@info-spectrum.com>
To: marchesini@unipg.it
To: pgsql-sql@postgresql.org
Subject: Re: foreign key problem
Date: Wed, 26 Sep 2007 18:55:48 +0530
Message-ID: <00c501c80040$cb2bee60$170211ac@LIONKING.COM> (raw)
References: <1190637286.4804.72.camel@ivangeo>


----- Original Message ----- 
From: "ivan marchesini" <marchesini@unipg.it>
To: <pgsql-sql@postgresql.org>
Sent: Monday, September 24, 2007 6:04 PM
Subject: [SQL] foreign key problem


> Dear users,
> I'm trying to create constraints into a db they give me from access...
> I'm using pgadmin to create a new foreign key for the relationship
> between two table..
>
> I have a table called "generalita" wich contains a column called
> "cod_carg":
> ___________________
> select cod_carg from generalita group by cod_carg;
> cod_carg
> ----------
>
> 2
> 0
> 1
> (4 righe)
> ___________________
>
>
> I have another table, called "diz_carg" that contain, into a column
> called "cod_carg" (that is a primary key), the domain values for the
> "cod_carg" column of "generalita":
> __________________
> select cod_carg from diz_carg group by cod_carg;
> cod_carg
> ----------
> 1
> 0
> 2
> (3 righe)
> __________________
>
>
>
> so when I try to create the foreign key I obtain:
> ALTER TABLE generalita ADD CONSTRAINT fkey_diz_cod_carg FOREIGN KEY
> (cod_carg) REFERENCES diz_carg (cod_carg) ON UPDATE NO ACTION ON DELETE
> NO ACTION;
>
> ____________________________
> ERROR:  insert or update on table "generalita" violates foreign key
> constraint "fkey_diz_cod_carg"
> DETAIL:  Key (cod_carg)=(  ) is not present in table "diz_carg".
> ____________________________
>
>
> can I allow null values for "generalita.cod_carg" although it is a
> foreign key for "diz_carg.cod_carg"?
>
> I apologize if it is a stupid question....   :-(
>
> many thanks to all
>
>


Yes you can , check that column cod_carg of table generalita is not set to 
NOT NULL.

With regards Ashish




view thread (11+ messages)

Message-ID: <00c501c80040$cb2bee60$170211ac@LIONKING.COM>
Permalink:  ../00c501c80040$cb2bee60$170211ac@LIONKING.COM/
Also on:    postgresql.org/message-id/00c501c80040$cb2bee60$170211ac@LIONKING.COM

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ashish.karalkar@info-spectrum.com, marchesini@unipg.it
  Subject: Re: foreign key problem
  In-Reply-To: <00c501c80040$cb2bee60$170211ac@LIONKING.COM>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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