Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with the foreign key

Status
Not open for further replies.

yp56

Programmer
Aug 30, 2001
83
FR
I have an amazing problem with postgresql
foreign key. I have postgresql 7.1.
that's my problem :
I have a table which contains a foreign key.
When I want to insert into this table I have a foreign key error that tells me that the values don't exists into the another table. But I'm sure that they exist. MOreover, I haven't got this error for all values (however i don't use special caracters only a-zéè).
There is what the serveur send to me :
(I have had comments)

--------------------------------------------
//this is the parameter's value transmit to the function
the school name: mon établissement -- the town : vannes

// I make a test to be sure that the value exist
the test sql request: select * from etablissement where e_libelle='mon établissement' AND e_localite='vannes'

// It exists since I have the third value of the etablissement table (ubs)
res test :: mon établissement -- vannes -- ubs
// and there is no error
tst error ::

// That's the request I want to do
// (the values (mon établissement, vannes) are the same since it's the same function)
the SQL request :
INSERT INTO RDipEta VALUES (222, 113, 0, 'A', 'BB', 'mon établissement', 'vannes', TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, '');

// that's the error i receive
the generated error :ERROR: fkrde_etablissementkey_ins referential integrity violation - key referenced from rdipeta not found in etablissement
--------------------------------------------

!! BUT the first request make sure that the reference exist !!!
What does it means...


There the function I talk about :

------------------------------------------
function insertRelationDiplomeEtablissement($Rde_NivCod, $Rde_GroupCod, $Rde_ForNum, $Rde_LettreCod, $Rde_TypeForCod, $Rde_Mot, $Rde_Localite, $Rde_FInitiale, $Rde_FContinue, $Rde_TPlein, $Rde_Correspondance, $Rde_Apprentissage, $Rde_Alternance, $Rde_Qualification, $Rde_Autre) {
GLOBAL $DB, $q;
// The parameters
print(&quot;the school name: $Rde_Mot -- the town : $Rde_Localite<BR>&quot;);

// test if the school exists into the etablissement table
$tst = &quot;select * from etablissement where e_libelle='$Rde_Mot' AND e_localite='$Rde_Localite'&quot;;
print(&quot;the test sql request: $tst <BR>&quot;);
$q = new query($DB,$tst);
$tst = $q->getrow();
// print the result of the test (it works good since i have the structure ratchment, the third value)
print(&quot;res test :: $tst[0] -- $tst[1] -- $tst[2]<BR>&quot;);
// print the error of the test
print(&quot;tst error :: &quot;.$q->error().&quot;<BR>&quot;);

// the query that I want execute
$sSQL = &quot;INSERT INTO RDipEta VALUES
($Rde_NivCod, $Rde_GroupCod, $Rde_ForNum, '$Rde_LettreCod', '$Rde_TypeForCod', '$Rde_Mot', '$Rde_Localite', $Rde_FInitiale, $Rde_FContinue, $Rde_TPlein, $Rde_Correspondance, $Rde_Apprentissage, $Rde_Alternance, $Rde_Qualification, '$Rde_Autre');&quot;;
$q = new query($DB, $sSQL);
print(&quot;the SQL request : <BR>$sSQL<BR>&quot;);
print(&quot;the generated error :&quot;.$q->error().&quot;<BR>&quot;);
return $q;
}
---------------------------------------------

More over sometimes it work :
(there are values with wich it works and another with wich it doesn't work...)
ex :
--------------------------------------------
the school name: l\'établissement -- the town : vannes

the test sql request: select * from etablissement where e_libelle='l\'établissement' AND e_localite='vannes'

res test :: l'établissement -- vannes -- ubs
tst error ::

the SQL request :
INSERT INTO RDipEta VALUES (222, 113, 0, 'A', 'BB', 'l\'établissement', 'vannes', TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, '');

// There is no error !!!
the generated error :
---------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top