Firebird SQL Development
 
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 ForumsDatabasesFirebird SQL Development

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 December 22nd, 2012, 11:19 AM
jwcane jwcane is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 22 jwcane User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 52 m 18 sec
Reputation Power: 0
Insert Query Fails

Running the query in IB_SQL succeeds, but perhaps too well:

insert into contact
(kCont,fkres,emFirst)
Values(-1,26,'Julie')

Primary key value setting is taken literally, when in fact what is desired is to generate the next key value, 1 in this case.

Hosting package developer gave me the default value for PK of -1, which is intended to activate the generator to provide the next available PK value.

From creation script for this table:

CREATE GENERATOR GENCONTACTNO;
SET GENERATOR GENCONTACTNO TO 0;

CREATE TABLE CONTACT (
KCONT INTEGER NOT NULL, /*etc.*/

UPDATE RDB$RELATION_FIELDS SET RDB$DESCRIPTION = 'pk="autoincrement" label="##" type="hidden"' WHERE (RDB$RELATION_NAME = 'CONTACT') AND (RDB$FIELD_NAME = 'KCONT');

ALTER TABLE CONTACT ADD CONSTRAINT RESIDENTS_CONTACT
FOREIGN KEY (FKRES) REFERENCES RESIDENTS (KRES);

=============

Running the query in Delphi generates following error
Violation of Primary or Unique key constraint "INTEG_17" on table "CONTACT".

Don't know how to interpret that. Does INTEG_17 refer to the PK or FK contraint?

Comments appreciated.

Reply With Quote
  #2  
Old February 13th, 2013, 02:03 AM
nagysz nagysz is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 200 nagysz User rank is Sergeant (500 - 2000 Reputation Level)nagysz User rank is Sergeant (500 - 2000 Reputation Level)nagysz User rank is Sergeant (500 - 2000 Reputation Level)nagysz User rank is Sergeant (500 - 2000 Reputation Level)nagysz User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 11 h 19 m 28 sec
Reputation Power: 13
Well, i think there is a trigger missing which activates the generator on input of kCont=-1 value. We usually use NULL but -1 is ok too....your trigger should be like this:

CREATE OR ALTER TRIGGER contact_bi FOR contact
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.kCont=-1) THEN
NEW.kCont = GEN_ID(GENCONTACTNO, 1);
END


this will increase the generator and give you the next available value.
if you have records in your table then take care to set a proper generator value (the biggest kCont value) at beginning








Quote:
Originally Posted by jwcane
Running the query in IB_SQL succeeds, but perhaps too well:

insert into contact
(kCont,fkres,emFirst)
Values(-1,26,'Julie')

Primary key value setting is taken literally, when in fact what is desired is to generate the next key value, 1 in this case.

Hosting package developer gave me the default value for PK of -1, which is intended to activate the generator to provide the next available PK value.

From creation script for this table:

CREATE GENERATOR GENCONTACTNO;
SET GENERATOR GENCONTACTNO TO 0;

CREATE TABLE CONTACT (
KCONT INTEGER NOT NULL, /*etc.*/

UPDATE RDB$RELATION_FIELDS SET RDB$DESCRIPTION = 'pk="autoincrement" label="##" type="hidden"' WHERE (RDB$RELATION_NAME = 'CONTACT') AND (RDB$FIELD_NAME = 'KCONT');

ALTER TABLE CONTACT ADD CONSTRAINT RESIDENTS_CONTACT
FOREIGN KEY (FKRES) REFERENCES RESIDENTS (KRES);

=============

Running the query in Delphi generates following error
Violation of Primary or Unique key constraint "INTEG_17" on table "CONTACT".

Don't know how to interpret that. Does INTEG_17 refer to the PK or FK contraint?

Comments appreciated.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Insert Query Fails

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