#27062 closed defect (bug) (invalid)
dbDelta error on plugin activation/deactivation/reactivation
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.8.1 |
Component: | Database | Keywords: | |
Focuses: | Cc: |
Description (last modified by )
When a plugin creates a table on activation via dbDelta, dbDelta results in an error on a plugin deactivation/reactivation cycle.
The attached minimalist code demonstrates the problem. The plugin creates the table fine, on reactivation the following error results:
<div id='error'> <p class='wpdberror'><strong>WordPress database error:</strong> [Multiple primary key defined]<br /> <code>ALTER TABLE testsite_minimalist ADD PRIMARY KEY id (id)</code></p> </div>
Expected result is no error: dbDelta should not try to define the primary key a second time (or should drop the primary key before attempting to alter the table).
Attachments (1)
Change History (4)
#2
@
11 years ago
- Resolution set to invalid
- Status changed from new to closed
According to the MySQL manual, the syntax for the PRIMARY KEY
is like this:
PRIMARY KEY [index_type] (index_col_name,...)
And for reference, the index_type
and index_col_name
definitions:
index_col_name: col_name [(length)] [ASC | DESC] index_type: USING {BTREE | HASH}
So your SQL is not correct:
PRIMARY KEY id (id)
I've actually been doing mine the same way, but now I realize it's not right. You can't name a PRIMARY
key, it's name is always PRIMARY
(1). The only think that should be between there is the index_type
. So the correct syntax is:
PRIMARY KEY (id)
And indeed, this works with dbDelta()
and doesn't cause this error.
Minimalist demonstration of dbDelta error