PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 25th, 2003, 08:12 AM
wickwack wickwack is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 1 wickwack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post Foreign Key Constraints in Functions

I've written a stored procedure that performs an insert, and then another insert. See below. The foreign key constraint of the DeclarationBodies table always complains, meaning the declaration_id is not present in the Declarations table... But it should be, given that the select query finds it...

Any Ideas?

Code:
CREATE TABLE Declarations (
  "declaration_id" SERIAL NOT NULL, 
  "name" VARCHAR(100) NOT NULL, 
  "theory_id" INTEGER NOT NULL,
  "version" VARCHAR(50) DEFAULT (''),
  "pvs_version" VARCHAR(50) DEFAULT (''), 
  CONSTRAINT pk_Declarations_declaration_id
  	PRIMARY KEY("declaration_id"), 
  CONSTRAINT fk_Declarations_theory_id
  	FOREIGN KEY ("theory_id") REFERENCES Theories("theory_id")
		ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE
);

CREATE TABLE DeclarationBodies (
  "declaration_id" INTEGER NOT NULL, 
  "body" TEXT, 
  CONSTRAINT fk_Declaration_Bodies_declaration_id
  	FOREIGN KEY ("declaration_id") REFERENCES Declarations("declaration_id")
		ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE
);

CREATE TABLE Functions(
  "signature" VARCHAR(100),
  CONSTRAINT pk_Functions_declaration_id
  	PRIMARY KEY("declaration_id"), 
  CONSTRAINT fk_Functions_theory_id
  	FOREIGN KEY ("theory_id") REFERENCES Theories("theory_id")
		ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE
) INHERITS(Declarations);

CREATE OR REPLACE FUNCTION fn_insert_function(integer,varchar,integer,varchar,text) RETURNS integer AS'
DECLARE
  in_received_id ALIAS FOR $1;
  in_name ALIAS FOR $2;
  in_theory_id ALIAS FOR $3;
  in_signature ALIAS FOR $4;
  in_body ALIAS FOR $5;
  res_oid INTEGER;
  d_id INTEGER;
--  r_row record;  
BEGIN
--  SELECT INTO r_row R.version, R.pvs_version
--    FROM Received R
--    WHERE R.received_id = in_received_id;
  
--  IF NOT FOUND THEN
--    RAISE EXCEPTION ''Received id [%] not found!  Aborting insert of [%].'',in_received_id,in_name;
--    RETURN -1;  
--  END IF;

  INSERT INTO Functions (name, theory_id, signature)
    VALUES (in_name, in_theory_id, in_signature);
  
  GET DIAGNOSTICS res_oid = RESULT_OID;
  
  SELECT INTO d_id F.declaration_id 
    FROM Functions F
    WHERE F.oid = res_oid;
    
  RAISE NOTICE ''Declaration ID: %'',d_id;
  
  INSERT INTO DeclarationBodies (declaration_id, body) VALUES (d_id, in_body);

  RETURN d_id;
END;
'LANGUAGE 'plpgsql' STABLE RETURNS NULL ON NULL INPUT SECURITY INVOKER;


Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Foreign Key Constraints in Functions

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap