tags:

views:

53

answers:

2

I have a problem with mysql duplicate error messages. On one of my machines the error looks like:

Duplicate entry 'foobar' for key 2

on the other machine it looks like:

Duplicate entry 'foobar' for name

  • One machine prints the key index, the other the key name.
  • One machine is an Ubuntu 10.04 Desktop; the other a Debian Lenny Server
  • MySQL Versions:

Ubuntu: Server version: 5.1.41-3ubuntu12.6 Protocol version: 10

Debian: Server version: 5.0.51a-24+lenny1-log Protocol version: 10

Should this be really a version issue? Any suggestions how I can control this?

+2  A: 

I'm guessing that you are attempting to read the error message in your code and take appropriate action and that the differing messages is causing a headache in trying to decide what action to take. If so, I suggest that you use the mysql error code rather than the error message. The message text will probably change with different software releases and language translations, but the code should always stay the same. In php you can check the code with:

mysql_errno($dbh);

I believe the error code that corresponds to that message is 1062.

spuriousdata
Yes, its possible to detect that it is a duplicate key issue this way. But you cant get information about what key is meant. (Maybe there is one that I dont know?) In my application I have to display an error message like: "You have selected the *name* twice" or "The *ip* has been already selected. Use another one!" ......
thorsten
I see. So this table has a multi-column primary key and you need to figure out which column caused the error?
spuriousdata
Not exactly. There are mutliple unique key's (multi- and single-column) and I want to figure out the name of the key or its index (either *unique_name* or *key 2*) . But I'll be sure that the behaviour is the same on all machines.
thorsten
I looked around a little bit and I wasn't able to find any way to get the field that caused the error in a consistent manner. You might be able to do something a little bit more complex using pdo_mysql (if you're not already); other than that, I can't think of anything.
spuriousdata
Thanks for looking around! Yes I use pdo_mysql. Next step I'll have to look if the error message is a server or a client issue. I'll post my answers if any.
thorsten
A: 

Hi there,

after digging through the mysql server source code I can say this is a version issue.

version 5.0.41: the error_dump method looks similar to:

dump_error(..,.., key.nr +1);

version 5.1.51: the error_dump method looks similar to:

dump_error(..,.., key.name);

So there is no reliable way to detect what key is a doublette with mysql?

thorsten