New Posts New Posts RSS Feed: ConstraintException problem
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

ConstraintException problem

 Post Reply Post Reply
Author
kjohnson View Drop Down
Newbie
Newbie


Joined: 19-Nov-2008
Posts: 19
Post Options Post Options   Quote kjohnson Quote  Post ReplyReply Direct Link To This Post Topic: ConstraintException problem
    Posted: 19-Oct-2009 at 6:26am
I've started getting a ConstraintException periodically when when I create a new object through the persistence manager.  The problem started this past Friday, usually on one of the first objects my program creates the Model hits the exception with a message of "Column 'Id' is constrained to be unique.  Value '-3' is already present."  the exception is thrown on the statement 'aReceiptDetail.AddToManager();" and the only time the column 'Id' is touched is at "pPersMgr.GenerateId(aReceiptDetail, ReceiptDetail.IdEntityColumn);".  The way that this object is generated has not changed significantly is several weeks, and the NumericIdGenerator has not changed since last year, but for some reason it occasionally assigns an in-use tempid to a newly created object.  Any ideas on what could be causing this?
Back to Top
kjohnson View Drop Down
Newbie
Newbie


Joined: 19-Nov-2008
Posts: 19
Post Options Post Options   Quote kjohnson Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2009 at 7:13am
After doing some more investigating I believe I have found the cause.  Somehow an object of the type 'ReceiptDetail' was saved to the database with an Id of '-3'.  Correcting this should correct the problem.
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2009 at 1:34pm
Be careful!   My concern is that you may be fixing the symptom and have not addressed the root cause.
 
Very rarely do we see reports of entities being saved to the database with negative parmanent ids.  This can be a very serious problem, and the problem can propagate and go worse over time.  One way that this could happen would be if you had a grandparent-parent-child relationship and failed to include all 3 relationships in the Object Mapper.  In any case, I would strongly recommend that you try to prevent this error from occurring during the save (perhaps with a DB constraint).
Back to Top
kjohnson View Drop Down
Newbie
Newbie


Joined: 19-Nov-2008
Posts: 19
Post Options Post Options   Quote kjohnson Quote  Post ReplyReply Direct Link To This Post Posted: 19-Oct-2009 at 2:07pm
I didn't want to make it sound like I'm taking a problem lightly.  Since I know that objects are saved to the DB with a positive permanent id I wanted to try and make sure that it wouldn't happen again.  I had been working on adding a new feature to our project when the ConstraintException started happening.  After I removed the modified code from being executed the exception continued to occur, which is what I originally posted about.  I have since done testing while adding the modified code back in to try and prevent the problem occurring again.  So far I have not seen it happen a second time.

I don't think that a missing relationship in the Object Mapper could be the cause, but I will look into that and implementing DB constraints to try and prevent this happening again.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down