Print Page | Close Window

ConstraintException problem

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1524
Printed Date: 28-Apr-2025 at 5:17am


Topic: ConstraintException problem
Posted By: kjohnson
Subject: ConstraintException problem
Date 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?



Replies:
Posted By: kjohnson
Date 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.


Posted By: davidklitzke
Date 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).


Posted By: kjohnson
Date 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.



Print Page | Close Window