Board index » delphi » Violation error adding new record using generator field

Violation error adding new record using generator field

Hi
I get the error 'Violation of PRIMARY or UNIQUE KEY constraint INTEG_45 on
table USERS' when i try to add a record to a empty table. Probably I have
missunderstod something and I hope somebody can help me.

The field USERID is the primary field and I have created a generator in IB
and set it to 1001 in order to get unique autoincremented numbers to insert.

As I can understand, the property GeneratorField of TIBDataSet has been
added
to help inserting values in PRIMARY KEYS without using triger procedures in
IB6.

My IBDataSet1 is connected to a IBDatabase1

IBDataset1 is linked to IBTransaction1 and IBUpdateSQL1
CachedUpdate = True
GeneratorField = USID_GEN -> USERID By 1  ; Applay Event OnPost
InsertSQL = insert into USERS
           (USERID, USERNAME, FIRSTNAME, LASTNAME, EMAIL, PWD)
            values
           (:USERID, :USERNAME, :FIRSTNAME, :LASTNAME, :EMAIL, :PWD)

IBTransaction1.Params set to
read_committed
rec_version
nowait

IBUpdateSQL insertSQL is the same as that of IBDataSet1

My procedure to insert a new record looks like this:

with IBDataSet1 do begin
   Append;
   FieldByName('USERNAME').AsString := UserName;
   FieldByName('FIRSTNAME').AsString := FirstName;
   FieldByName('LASTNAME').AsString := LastName;
   FieldByName('EMAIL').AsString := Email;
   FieldByName('PWD').AsString := pwd;
   Post;
   ApplyUpdates; {here the violation error occure}
 end;

Table declaration looks like this:

CREATE DOMAIN TEMAILADR AS VARCHAR(40);
CREATE DOMAIN TFIRSTNAME AS VARCHAR(15);
CREATE DOMAIN TLASTNAME AS VARCHAR(20);
CREATE DOMAIN TUSERID AS INTEGER
  CHECK (VALUE > 1000);
CREATE DOMAIN TUSERNAME AS VARCHAR(15);

CREATE TABLE USERS
(
  USERID TUSERID NOT NULL,
  USERNAME TUSERNAME NOT NULL,
  FIRSTNAME TFIRSTNAME,
  LASTNAME TLASTNAME,
  EMAIL TEMAILADR,
  PWD VARCHAR(12) NOT NULL,
 UNIQUE (USERNAME),
 PRIMARY KEY (USERID)
);
CREATE GENERATOR USID_GEN;

I use Delphi 5.01, IB6 dialect 1, IBX 4.41

By the way
When is the the protected procedure DoOnNewRecord called in the
TIBCustomDataSet

/Lars

 

Re:Violation error adding new record using generator field


First off, in your case the IBUpdateSQL is not needed.  It is more there for the
people who need to update multiple tables in the OnUpdateRecord event.  You
aren't doing this and the straight InsertSQL is all you need.

The bug it not in the GeneratorField property.  It is in your definition.  Your
domain for the USERID column is

CREATE DOMAIN TUSERID AS INTEGER
  CHECK (VALUE > 1000);

yet the Generator is starting at 1.  Try executing

set generator  usid_gen to 1000

now run and it should work just fine.  Does for me.  It was your check
constraint that was firing because you did not set the generator to start at
1000.

Quote
Lars wrote:

> Hi
> I get the error 'Violation of PRIMARY or UNIQUE KEY constraint INTEG_45 on
> table USERS' when i try to add a record to a empty table. Probably I have
> missunderstod something and I hope somebody can help me.

> The field USERID is the primary field and I have created a generator in IB
> and set it to 1001 in order to get unique autoincremented numbers to insert.

> As I can understand, the property GeneratorField of TIBDataSet has been
> added
> to help inserting values in PRIMARY KEYS without using triger procedures in
> IB6.

> My IBDataSet1 is connected to a IBDatabase1

> IBDataset1 is linked to IBTransaction1 and IBUpdateSQL1
> CachedUpdate = True
> GeneratorField = USID_GEN -> USERID By 1  ; Applay Event OnPost
> InsertSQL = insert into USERS
>            (USERID, USERNAME, FIRSTNAME, LASTNAME, EMAIL, PWD)
>             values
>            (:USERID, :USERNAME, :FIRSTNAME, :LASTNAME, :EMAIL, :PWD)

> IBTransaction1.Params set to
> read_committed
> rec_version
> nowait

> IBUpdateSQL insertSQL is the same as that of IBDataSet1

> My procedure to insert a new record looks like this:

> with IBDataSet1 do begin
>    Append;
>    FieldByName('USERNAME').AsString := UserName;
>    FieldByName('FIRSTNAME').AsString := FirstName;
>    FieldByName('LASTNAME').AsString := LastName;
>    FieldByName('EMAIL').AsString := Email;
>    FieldByName('PWD').AsString := pwd;
>    Post;
>    ApplyUpdates; {here the violation error occure}
>  end;

> Table declaration looks like this:

> CREATE DOMAIN TEMAILADR AS VARCHAR(40);
> CREATE DOMAIN TFIRSTNAME AS VARCHAR(15);
> CREATE DOMAIN TLASTNAME AS VARCHAR(20);
> CREATE DOMAIN TUSERID AS INTEGER
>   CHECK (VALUE > 1000);
> CREATE DOMAIN TUSERNAME AS VARCHAR(15);

> CREATE TABLE USERS
> (
>   USERID TUSERID NOT NULL,
>   USERNAME TUSERNAME NOT NULL,
>   FIRSTNAME TFIRSTNAME,
>   LASTNAME TLASTNAME,
>   EMAIL TEMAILADR,
>   PWD VARCHAR(12) NOT NULL,
>  UNIQUE (USERNAME),
>  PRIMARY KEY (USERID)
> );
> CREATE GENERATOR USID_GEN;

> I use Delphi 5.01, IB6 dialect 1, IBX 4.41

> By the way
> When is the the protected procedure DoOnNewRecord called in the
> TIBCustomDataSet

> /Lars

--
Jeff Overcash (TeamB)   | Talk about failure
(Please do not email    | To fall is not to fail
 me directly unless     | Failure isn't about falling down
 asked.  Thank You)     | Failure is staying down (Marillion)

Re:Violation error adding new record using generator field


Hi Jeff

The generator is set to 1001 as I wrote in my message and I can see when
debuging that the generator gets incremented and a number bigger than 1000
returns to the program (ISAPI) from IB.
Still I get the violation error so something else must be wrong in my code.
The IBDataset is not linked to a grid if it matter.
Another problem, I tried using generator Apply event OnNewRecord, but
DoOnNewRecord is never called in TIBCustomDataSet as I can see.

Thanks for offering your time.

/Lars

"Jeff Overcash (TeamB)" <overc...@onramp.net> skrev i meddelandet
news:3A3C3CC6.3F25124D@onramp.net...

Quote
> First off, in your case the IBUpdateSQL is not needed.  It is more there
for the
> people who need to update multiple tables in the OnUpdateRecord event.
You
> aren't doing this and the straight InsertSQL is all you need.

> The bug it not in the GeneratorField property.  It is in your definition.
Your
> domain for the USERID column is

> CREATE DOMAIN TUSERID AS INTEGER
>   CHECK (VALUE > 1000);

> yet the Generator is starting at 1.  Try executing

> set generator  usid_gen to 1000

> now run and it should work just fine.  Does for me.  It was your check
> constraint that was firing because you did not set the generator to start
at
> 1000.

> Lars wrote:

> > Hi
> > I get the error 'Violation of PRIMARY or UNIQUE KEY constraint INTEG_45
on
> > table USERS' when i try to add a record to a empty table. Probably I
have
> > missunderstod something and I hope somebody can help me.

> > The field USERID is the primary field and I have created a generator in
IB
> > and set it to 1001 in order to get unique autoincremented numbers to
insert.

> > As I can understand, the property GeneratorField of TIBDataSet has been
> > added
> > to help inserting values in PRIMARY KEYS without using triger procedures
in
> > IB6.

> > My IBDataSet1 is connected to a IBDatabase1

> > IBDataset1 is linked to IBTransaction1 and IBUpdateSQL1
> > CachedUpdate = True
> > GeneratorField = USID_GEN -> USERID By 1  ; Applay Event OnPost
> > InsertSQL = insert into USERS
> >            (USERID, USERNAME, FIRSTNAME, LASTNAME, EMAIL, PWD)
> >             values
> >            (:USERID, :USERNAME, :FIRSTNAME, :LASTNAME, :EMAIL, :PWD)

> > IBTransaction1.Params set to
> > read_committed
> > rec_version
> > nowait

> > IBUpdateSQL insertSQL is the same as that of IBDataSet1

> > My procedure to insert a new record looks like this:

> > with IBDataSet1 do begin
> >    Append;
> >    FieldByName('USERNAME').AsString := UserName;
> >    FieldByName('FIRSTNAME').AsString := FirstName;
> >    FieldByName('LASTNAME').AsString := LastName;
> >    FieldByName('EMAIL').AsString := Email;
> >    FieldByName('PWD').AsString := pwd;
> >    Post;
> >    ApplyUpdates; {here the violation error occure}
> >  end;

> > Table declaration looks like this:

> > CREATE DOMAIN TEMAILADR AS VARCHAR(40);
> > CREATE DOMAIN TFIRSTNAME AS VARCHAR(15);
> > CREATE DOMAIN TLASTNAME AS VARCHAR(20);
> > CREATE DOMAIN TUSERID AS INTEGER
> >   CHECK (VALUE > 1000);
> > CREATE DOMAIN TUSERNAME AS VARCHAR(15);

> > CREATE TABLE USERS
> > (
> >   USERID TUSERID NOT NULL,
> >   USERNAME TUSERNAME NOT NULL,
> >   FIRSTNAME TFIRSTNAME,
> >   LASTNAME TLASTNAME,
> >   EMAIL TEMAILADR,
> >   PWD VARCHAR(12) NOT NULL,
> >  UNIQUE (USERNAME),
> >  PRIMARY KEY (USERID)
> > );
> > CREATE GENERATOR USID_GEN;

> > I use Delphi 5.01, IB6 dialect 1, IBX 4.41

> > By the way
> > When is the the protected procedure DoOnNewRecord called in the
> > TIBCustomDataSet

> > /Lars

> --
> Jeff Overcash (TeamB)   | Talk about failure
> (Please do not email    | To fall is not to fail
>  me directly unless     | Failure isn't about falling down
>  asked.  Thank You)     | Failure is staying down (Marillion)

Re:Violation error adding new record using generator field


Quote
Lars wrote:

> Hi Jeff

> The generator is set to 1001 as I wrote in my message and I can see when
> debuging that the generator gets incremented and a number bigger than 1000
> returns to the program (ISAPI) from IB.

All I know is the moment I set the generator to 1000 everything worked just fine
as expected.  So your steps did not reproduce here either with or without the
IBUpdateSQL.

Quote
> Still I get the violation error so something else must be wrong in my code.
> The IBDataset is not linked to a grid if it matter.
> Another problem, I tried using generator Apply event OnNewRecord, but
> DoOnNewRecord is never called in TIBCustomDataSet as I can see.

DoOnNewRecord is called from the TDataset level, not TIBCustomDataset.  It is
called in the EndInsertAppend (which is when it would get called in an Insert or
Append call) or in AddRecord (which is not a part of this).  IBCustomDataset
does not directly call it.  I placed this code into a OnNewRecord event for the
Dataset and it always fired just fine.

procedure TForm1.IBDataSet1NewRecord(DataSet: TDataSet);
begin
  ShowMessage(DataSet.FieldByName('userid').AsString);
end;

The only violation I ever saw was on on the unique constraint for username when
I duplicated usernames.

Quote
> Thanks for offering your time.

> /Lars

--
Jeff Overcash (TeamB)   | Talk about failure
(Please do not email    | To fall is not to fail
 me directly unless     | Failure isn't about falling down
 asked.  Thank You)     | Failure is staying down (Marillion)

Other Threads