tags:

views:

52

answers:

4

What advice can you give me on how to track down a bug I have while inserting data into MySQL database with .Net?

The error message is:

MySql.Data.MySqlClient.MySqlException: Duplicate entry '26012' for key 'StockNumber_Number_UNIQUE'

Reviewing of the log proves that StockNumber_Number of 26012 has not been inserted yet.

Products in use.

  • Visual Studio 2008.
  • mysql.data.dll 6.0.4.0.
  • Windows 7 Ultimate 64 bit and Windows 2003 32 bit.
  • Custom built ORM framework (have source code).
  • Importing data from Access 2003 database.

The code works fine for 3000 - 5000 imports.

The record being imported that causes the problem in a full run works fine if just importing by itself. I've also seen the error on other records if I sort the data to be imported a different way.

Have tried import with and without transactions.

Have logged the hell out of the system.

The SQL command to create the table:

CREATE TABLE `RareItems_RareItems` (
    `RareItemKey` CHAR(36) NOT NULL PRIMARY KEY, 
    `StockNumber_Text` VARCHAR(7) NOT NULL, 
    `StockNumber_Number` INT NOT NULL AUTO_INCREMENT,  
    UNIQUE INDEX `StockNumber_Number_UNIQUE` (`StockNumber_Number` ASC), 
    `OurPercentage` NUMERIC ,  
    `SellPrice` NUMERIC(19, 2) ,  
    `Author` VARCHAR(250) ,  
    `CatchWord` VARCHAR(250) ,  
    `Title` TEXT ,  
    `Publisher` VARCHAR(250) ,  
    `InternalNote` VARCHAR(250) ,  
    `DateOfPublishing` VARCHAR(250) ,  
    `ExternalNote` LONGTEXT ,  
    `Description` LONGTEXT ,  
    `Scrap` LONGTEXT ,  
    `SuppressionKey` CHAR(36) NOT NULL,  
    `TypeKey` CHAR(36) NOT NULL,  
    `CatalogueStatusKey` CHAR(36) NOT NULL,  
    `CatalogueRevisedDate` DATETIME ,  
    `CatalogueRevisedByKey` CHAR(36) NOT NULL,  
    `CatalogueToBeRevisedByKey` CHAR(36) NOT NULL,  
    `DontInsure` BIT NOT NULL,  
    `ExtraCosts` NUMERIC(19, 2) ,  
    `IsWebReady` BIT NOT NULL,  
    `LocationKey` CHAR(36) NOT NULL,  
    `LanguageKey` CHAR(36) NOT NULL,  
    `CatalogueDescription` VARCHAR(250) ,  
    `PlacePublished` VARCHAR(250) ,  
    `ToDo` LONGTEXT ,  
    `Headline` VARCHAR(250) ,  
    `DepartmentKey` CHAR(36) NOT NULL,  
    `Temp1` INT ,  
    `Temp2` INT ,  
    `Temp3` VARCHAR(250) ,  
    `Temp4` VARCHAR(250) ,  
    `InternetStatusKey` CHAR(36) NOT NULL,  
    `InternetStatusInfo` LONGTEXT ,  
    `PurchaseKey` CHAR(36) NOT NULL,  
    `ConsignmentKey` CHAR(36) ,  
    `IsSold` BIT NOT NULL,  
    `RowCreated` DATETIME NOT NULL,  
    `RowModified` DATETIME NOT NULL
);

The SQL command and parameters to insert the record:

INSERT INTO `RareItems_RareItems` 
    (`RareItemKey`, `StockNumber_Text`, `StockNumber_Number`, `OurPercentage`, `SellPrice`, `Author`, `CatchWord`, `Title`, `Publisher`, `InternalNote`, `DateOfPublishing`, `ExternalNote`, `Description`, `Scrap`, `SuppressionKey`, `TypeKey`, `CatalogueStatusKey`, `CatalogueRevisedDate`, `CatalogueRevisedByKey`, `CatalogueToBeRevisedByKey`, `DontInsure`, `ExtraCosts`, `IsWebReady`, `LocationKey`, `LanguageKey`, `CatalogueDescription`, `PlacePublished`, `ToDo`, `Headline`, `DepartmentKey`, `Temp1`, `Temp2`, `Temp3`, `Temp4`, `InternetStatusKey`, `InternetStatusInfo`, `PurchaseKey`, `ConsignmentKey`, `IsSold`, `RowCreated`, `RowModified`) 
VALUES
    (@RareItemKey, @StockNumber_Text, @StockNumber_Number, @OurPercentage, @SellPrice, @Author, @CatchWord, @Title, @Publisher, @InternalNote, @DateOfPublishing, @ExternalNote, @Description, @Scrap, @SuppressionKey, @TypeKey, @CatalogueStatusKey, @CatalogueRevisedDate, @CatalogueRevisedByKey, @CatalogueToBeRevisedByKey, @DontInsure, @ExtraCosts, @IsWebReady, @LocationKey, @LanguageKey, @CatalogueDescription, @PlacePublished, @ToDo, @Headline, @DepartmentKey, @Temp1, @Temp2, @Temp3, @Temp4, @InternetStatusKey, @InternetStatusInfo, @PurchaseKey, @ConsignmentKey, @IsSold, @RowCreated, @RowModified)

@RareItemKey = 0b625bd6-776d-43d6-9405-e97159d172a6
@StockNumber_Text = 199305
@StockNumber_Number = 26012
@OurPercentage = 22.5
@SellPrice = 1250
@Author = SPARRMAN, Anders.
@CatchWord = COOK: SECOND VOYAGE
@Title = A Voyage Round the World with Captain James Cook in H.M.S. Resolution… Introduction and notes by Owen Rutter, wood engravings by Peter Barker-Mill.
@Publisher = 
@InternalNote = 
@DateOfPublishing = 1944
@ExternalNote = The first English translation of Sparrman’s narrative, which had originally     been published in Sweden in 1802-1818, and the only complete version of his account to appear in English. The eighteenth-century translation had appeared some time before the Swedish publication of the final sections of his account. Sparrman’s observant and well-written narrative of the second voyage contains much that appears nowhere else, emphasising naturally his interests in medicine, health, and natural history.<br><br>One of 350 numbered copies: a handsomely produced and beautifully illustrated work.
@Description = Small folio, wood-engravings in the text; original olive glazed cloth, top edges gilt, a very good copy. London, Golden Cockerel Press, 1944.
@Scrap = 
@SuppressionKey = 00000000-0000-0000-0000-000000000000
@TypeKey = 93f58155-7471-46ad-84c5-262ab9dd37e8
@CatalogueStatusKey = 00000000-0000-0000-0000-000000000003
@CatalogueRevisedDate = 
@CatalogueRevisedByKey = c4f6fc06-956d-44c4-b393-0d5462cbffec
@CatalogueToBeRevisedByKey = 00000000-0000-0000-0000-000000000000
@DontInsure = False
@ExtraCosts = 
@IsWebReady = False
@LocationKey = 00000000-0000-0000-0000-000000000000
@LanguageKey = 00000000-0000-0000-0000-000000000000
@CatalogueDescription = 
@PlacePublished = Golden Cockerel Press
@ToDo = 
@Headline = 
@DepartmentKey = 529578a3-9189-40de-b656-eef9039d00b8
@Temp1 = 
@Temp2 = 
@Temp3 = 
@Temp4 = v
@InternetStatusKey = 00000000-0000-0000-0000-000000000000
@InternetStatusInfo = 
@PurchaseKey = 00000000-0000-0000-0000-000000000000
@ConsignmentKey = 
@IsSold = True
@RowCreated = 8/04/2010 8:49:16 PM
@RowModified = 8/04/2010 8:49:16 PM

Suggestions on what is causing the error and/or how to track down what is causing the problem?

+1  A: 

Apologies, I'm a MSSQL bod so maybe off the mark here but why are you inserting StockNumber_Number as part of your insert. Its an auto_increment field so surely will be created for you.

Either drop this field from your insert or don't make it an auto_increment and the problem should go away.

CResults
Correct... the auto-increment's sole purpose is to auto-assign the next sequence. Its like a read-only column and trying to force a value will fail.
DRapp
I'm inserting the StockNumber_Number because I'm inserting legacy data and must keep the number. However I will test with auto_increment off.
Tim Murphy
Removing auto_increment during import has worked. I'll accept your answer after I do a little testing and implementation just in case something comes up.
Tim Murphy
Have accepted this answer as it was the first to help me find the workaround. See my own answer for actual code to get around the problem.
Tim Murphy
A: 

At first glance, the STOCK_NUMBER is autoincrement ie. each time you insert a new record, the value of this column will be incremented by 1 from the previous value & inserted by the database.

In which case, why do you need to pass it in as a parameter to the query?

If you do need to pass it in, I would remove the autoincrement & keep the column unique...

HTH.

Sunny
+1  A: 

Remove Stock_Number from your INSERT statement and you should be ok.

Ardman
A: 

As all the other answers have indicated the problem was inserting a value to the AUTO_INCREMENT column StockNumber_Number.

It seems weird that I was able to do it for a few thousand rows before the error would occur however here was my required workaround.

ALTER TABLE `rareitems_rareitems` CHANGE COLUMN `StockNumber_Number` `StockNumber_Number` INT(11) NOT NULL;

...
do the import
...

ALTER TABLE `rareitems_rareitems` CHANGE COLUMN `StockNumber_Number` `StockNumber_Number` INT(11) NOT NULL AUTO_INCREMENT;
Tim Murphy