yarmol / h2database

Automatically exported from code.google.com/p/h2database
0 stars 0 forks source link

H2 1.4.177: Unique Constraint exception returning objectName of TreeIndex instead of ConstraintUnique #560

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
1. Create a table with a unique constraint on a column like UK_TITLE.
2. Trigger the unique constraint error by inserting twice the same data.

Expected Message:

 - Unique index or primary key violation: "UK_TITLE ON PUBLIC.TEST(TITLE) VALUES (...)"

Actual Message:

 - Unique index or primary key violation: "UK_TITLE_INDEX_2 ON PUBLIC.TEST(TITLE) VALUES (...)"

Versions:
 - H2 1.4.177
 - Java 7

Workaround?
 - No

What is your use case, meaning why do you need this feature?

 - Incorrect behaviour

How important/urgent is the problem for you?

 - High, as we'd like to use the index name for internationalization we can currently not test this feature using H2 without special handling code.

Please provide any additional information below.

After some investigation I have discovered that the actual test is not done by 
the class ConstraintUnique but instead is delegated to the Index on that 
column. (Performance reason i believe?)

This index name is the constraint name plus _INDEX_(num), which is the used 
objectName for the creation of the DuplicateKeyException.

Possible solution (?):

As the code for DuplicateKeyException is implemented in the BaseIndex and this 
Index should always have a related constraint it would be possible to call 
getName() on the related Constraint? 

Heres part of the stacktrace: 

TreeIndex(BaseIndex).getDuplicateKeyException(String) line: 98  
TreeIndex.add(Session, Row) line: 69    
MultiVersionIndex.add(Session, Row) line: 59    
RegularTable.addRow(Session, Row) line: 120 
Insert.insertRows() line: 157 [local variables unavailable] 
Insert.update() line: 115   

Original issue reported on code.google.com by mfrey0@gmail.com on 29 Apr 2014 at 6:50

GoogleCodeExporter commented 9 years ago
Hi

So, you rely on the exception message to have a specific format?

You wrote "Incorrect behaviour". Why do you think the behavior is incorrect? I 
understand it may not be "convenient" for you, but incorrect would mean it is 
documented somewhere in H2. Is it documented?

Regards,
Thomas

Original comment by thomas.t...@gmail.com on 2 May 2014 at 7:31

GoogleCodeExporter commented 9 years ago
All I'm saying is that, if we define a constraint name abd there is an
exception for that constraint we should expect this name to be given
and not another one. Else why bother with names anyway, right?

If we have serveral constraints on one table and one is failing we
need to know which one. As we currently get an object name with is
different then what we declared I consider this a bug and not a
feature ;)

But you are right. It's not a high priority bug.

Original comment by mfrey0@gmail.com on 2 May 2014 at 10:13