Home » Developer & Programmer » Forms » Unique constraint violated (Forms 9i, Oracle 10g)
Unique constraint violated [message #449343] Mon, 29 March 2010 04:42 Go to next message
rosswelsh
Messages: 42
Registered: February 2010
Location: Sunderland
Member
Hi all, hope you can help.

I have a form that is used to insert data into my database. It has a data block based on the table it inserts data into. To enter the data I am using a save button with some code behind it.

I know that, because of the data block, Oracle will automatically insert the data for me, without needing to use a save button, but I need this button.

The problem is that when I attempt to click the save button I get the error unique constraint violated on either the index or for a constraint that has been set up on my primary key field (HeatNumber). Is this because the save button is attempting to enter the data and Oracle is also trying to do it at the same time, thus having 2 identical records?

I have tried removing both the index and constraint and the data inserts, but inserts 2 identical rows, which is why I think both the save button and Oracle are doing the same thing.

Can I get around this by removing the data block and just using a save button? I thought I would ask incase it wouldn't work and I would then have to re-do it all again.

Thanks for any advice.

Re: Unique constraint violated [message #449346 is a reply to message #449343] Mon, 29 March 2010 04:48 Go to previous messageGo to next message
cookiemonster
Messages: 13938
Registered: September 2008
Location: Rainy Manchester
Senior Member
The obvious questions are:
What is the code in the save button?
Why are you using it?
Re: Unique constraint violated [message #449349 is a reply to message #449343] Mon, 29 March 2010 04:52 Go to previous messageGo to next message
rosswelsh
Messages: 42
Registered: February 2010
Location: Sunderland
Member
The code is:

BEGIN
   INSERT INTO adddatarequest (heatnumber,
                               requestedby,
                               description,
                               typeofquery,
                               querydescription,
                               documenttitle,
                               datacrossref,
                               datadespatched,
                               userfollowup,
                               additionalinfo,
                               attachedsql,
                               workcompby,
                               datecomp,
                               confirmedby,
                               confirmeddate)
       VALUES (:heatnumber,
               :requestedby,
               :description,
               :typeofquery,
               :querydescription,
               :documenttitle,
               :datacrossref,
               :datadespatched,
               :userfollowup,
               :additionalinfo,
               :attachedsql,
               :workcompby,
               :datecomp,
               :confirmedby,
               :confirmeddate);


   COMMIT;
END;


and I am using the button because it is part of the requriements for my system.

[EDITED by LF: reformatted code]

[Updated on: Mon, 29 March 2010 04:58] by Moderator

Report message to a moderator

Re: Unique constraint violated [message #449351 is a reply to message #449343] Mon, 29 March 2010 04:54 Go to previous messageGo to next message
sinida1984
Messages: 83
Registered: September 2007
Location: India
Member
Hi

In the save button give 'Commit' only. No need of insert statements. If you want only one row, you need to make the 'Database block' to 'No' or give commit only at save button

Sinida

[Updated on: Mon, 29 March 2010 04:57]

Report message to a moderator

Re: Unique constraint violated [message #449358 is a reply to message #449349] Mon, 29 March 2010 05:01 Go to previous messageGo to next message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Quote:
Is this because the save button is attempting to enter the data and Oracle is also trying to do it at the same time, thus having 2 identical records?

Yes

Quote:
Can I get around this by removing the data block and just using a save button?

Yes, but that's not the way to do it.

Quote:
I know that, because of the data block, Oracle will automatically insert the data for me, without needing to use a save button, but I need this button.

Perhaps you need a button, but not INSERT statement behind it.

Once again: what's wrong with default Forms behaviour? Why do you think that you need to INSERT a record, when Forms do that perfectly well?
Re: Unique constraint violated [message #449365 is a reply to message #449343] Mon, 29 March 2010 05:10 Go to previous messageGo to next message
rosswelsh
Messages: 42
Registered: February 2010
Location: Sunderland
Member
I have removed the insert statement and just have commit behind the button and it appears to be working fine now.

I didn't realise you could just have commit, I thought you needed to have an insert statement as well. At least now I know!

Thanks for your help! Smile
Re: Unique constraint violated [message #449371 is a reply to message #449343] Mon, 29 March 2010 05:21 Go to previous messageGo to next message
cookiemonster
Messages: 13938
Registered: September 2008
Location: Rainy Manchester
Senior Member
Whenever you issue a commit in forms it applies all outstanding data block changes to the database - inserts, updates and commits. No code is needed on your part for this.

You'd normally only hard-code a DML statement in forms if you want to make changes to some table other than the one the block is based on.
So if you've got a table that logs inserts into the emp table for example you might put an insert statement in the pre-insert trigger on the emp block to insert into the log table.
Re: Unique constraint violated [message #449379 is a reply to message #449371] Mon, 29 March 2010 05:41 Go to previous messageGo to next message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
cookiemonster wrote on Mon, 29 March 2010 12:21
Whenever you issue a commit in forms it applies all outstanding data block changes to the database - inserts, updates and commits.

"commits" should have been "deletes", I presume?
Re: Unique constraint violated [message #449403 is a reply to message #449343] Mon, 29 March 2010 07:12 Go to previous message
cookiemonster
Messages: 13938
Registered: September 2008
Location: Rainy Manchester
Senior Member
D'oh - yes.
Previous Topic: Hierarchical Tree
Next Topic: Include multiple envFile
Goto Forum:
  


Current Time: Fri Sep 20 05:43:48 CDT 2024