Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#34471 closed defect (bug) (invalid)

dbDelta fails with existing tables and indices

Reported by: sourcerer-mike's profile sourcerer-mike Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Database Keywords:
Focuses: Cc:


Referring "wp-admin/includes/upgrade.php:2231" @WP 4.3.1

Take this Query:

CREATE TABLE wp_termmeta (
        meta_id bigint(20) NOT NULL AUTO_INCREMENT,
        term_id bigint(20) NOT NULL default 0,

        meta_key varchar(255) DEFAULT NULL,
        meta_value longtext DEFAULT NULL

   UNIQUE KEY meta_id (meta_id)

Running it multiple times with dbDelta will fail in an invalid Query:

ALTER TABLE wp_termmeta ADD

It creates queries for indices. But as I debug the variable $indices looks like this:

$indices = array(
  1 => "",
  2 => "",

I don't know why it is "empty" but this might be a hotfix:

$indices = array_filter($indices);

Change History (1)

#1 @pento
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version 4.3.1 deleted

Hi there!

This isn't a valid CREATE TABLE statement - the UNIQUE KEY line needs to be inside the brackets.

dbDelta() also doesn't play nicely with blank lines - see wp-admin/includes/schema.php for examples of how to write dbDelta()-friendly table definitions.

dbDelta() is not intended to parse all possible table definitions, just the subset needed for WordPress tables. While we're open to expanding what it supports, of course, for standard table definitions, you need to adhere to the formats it supports.

Note: See TracTickets for help on using tickets.