[wp-trac] [WordPress Trac] #10404: dbDelta creates duplicate indexes when index definition contains spaces

WordPress Trac noreply at wordpress.org
Tue Feb 26 18:50:29 UTC 2013


#10404: dbDelta creates duplicate indexes when index definition contains spaces
-------------------------------+-----------------------------
 Reporter:  Denis-de-Bernardy  |       Owner:
     Type:  enhancement        |      Status:  new
 Priority:  normal             |   Milestone:  Future Release
Component:  General            |     Version:  2.8.1
 Severity:  normal             |  Resolution:
 Keywords:  has-patch          |
-------------------------------+-----------------------------

Comment (by RonStrilaeff):

 I also get that this is old but it apparently there is still a problem
 with dbDelta creating duplicate indexes.  I am creating a table within the
 init action hook like this:

 {{{
 function sv_create_favorites_table(){
         global $wpdb;
         $table_name = $wpdb->prefix . "favorites";
         $sql = "CREATE TABLE IF NOT EXISTS $table_name (
                 fav_id bigint(20) not null auto_increment,
                 post_id bigint(20) not null,
                 user_id bigint(20) not null,
                 fav_date timestamp not null,
                 PRIMARY KEY  (fav_id),
                 KEY (post_id),
                 KEY (user_id)
                 );";

         require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
         dbDelta($sql);
 };

 add_action( 'init', 'sv_create_favorites_table');

 }}}

 ... and the only way I could prevent it from creating post_id_2, 3, 4, etc
 was to add the "IF NOT EXISTS" clause to the CREATE TABLE statement.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/10404#comment:6>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list