[wp-hackers] dbDelta Upgrade not working

Ajay D'Souza ajaydsouza at gmail.com
Wed Mar 11 05:28:03 GMT 2009


Hi,

I'm trying to upgrade the table structure for my plugin Top 10 to add an 
extra column to the existing table.

    // Upgrade table code
    $installed_ver = get_option( "tptn_db_version" );

    if( $installed_ver != $tptn_db_version ) {

     $sql = "CREATE TABLE " . $table_name . " (
         accessedid int NOT NULL AUTO_INCREMENT,
         postnumber int NOT NULL,
         cntaccess int NOT NULL,
         PRIMARY KEY  (accessedid)
     );";

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

     $sql = "CREATE TABLE " . $table_name_daily . " (
         accessedid int NOT NULL AUTO_INCREMENT,
         postnumber int NOT NULL,
         cntaccess int NOT NULL,
         dp_date date NOT NULL default CURDATE(),
         PRIMARY KEY  (accessedid)
     );";

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

       update_option( "tptn_db_version", $tptn_db_version );
   }

The extra column added is the dp_date date NOT NULL default CURDATE(), 
which isn't present in earlier versions. However, the upgrade isn't be 
processed, i.e. the extra column isn't added.

I've used the code from 
http://codex.wordpress.org/Creating_Tables_with_Plugins

The upgrade works only when I created a new table, but not to alter it.

Anybody have a clue as to why this isn't happening? Any solutions?

Thanks,
Ajay D'Souza


More information about the wp-hackers mailing list