Domanda

Similarly to many other questions about composite keys with AUTO_INCREMENT, I am receiving the following error:

Incorrect table definition; there can be only one auto column and it must be defined as a key

What I'm doing is making a history trail of all changes in the table. Every time a change is made, a new row is inserted with a new timestamp, leaving previous modifications untouched.

My DDL in concern is this:

DROP TABLE IF EXISTS personnel;
CREATE TABLE IF NOT EXISTS personnel
(
    modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    id INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,
    ...

    PRIMARY KEY (modified, id),
    ...
) ENGINE=InnoDB;

I presume that I'm getting this error because I am using both CURRENT_TIMESTAMP and AUTO_INCREMENT. So is there a way to resolve this without making PHP do the timestamp generation (so my model can just insert as if it were any other table, letting the controller do the dirty work).

È stato utile?

Soluzione

MySQL's error message has more to be desired...

The autoincrementing column has to be at the beginning of an index. In your case you need to add another index - KEY id(id).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top