Can someone tell me what's wrong with this function?

function xattachments_db_install() {
   global $wpdb;
   $xattachments_db_version = "1.0";
   $table_name = $wpdb->prefix ."xattachments";

   $sql = "CREATE TABLE IF NOT EXISTS ".$table_name. " (
        'xattachments_id' int(10) unsigned NOT NULL,
        'xattachments_name'  varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
        'xattachments_title' varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
        'post_id' int(10) unsigned NOT NULL,
        'xattachments_data_id' int(11) NOT NULL,
        PRIMARY KEY ('xattachments_id') 

   add_option("xattachments_db_version", $xattachments_db_version);     

It doesn't generate any error message when I activate the plugin but the table is not created in the database.

This function is located at the very beginning of the plugin file.

Thank you.

  • 1
    Code is OK as first look. Its should work. try to debug what sql running in dbDelta. Commented Nov 25, 2012 at 9:29
  • thank you Code Monkey. How would I do that? Commented Nov 25, 2012 at 9:30
  • Find location of dbDelta, check what arguments it gets, test that arguments (its sql which should run). Commented Nov 25, 2012 at 9:38

1 Answer 1


This part in your table creation SQL looks wrong: DEFAULT CHARSET=CHARSET=utf8. It should just be DEFAULT CHARSET=utf8.

Also, dbDelta() is quite a delicate function. Don't ask me why, but you need to put a double space after the PRIMARY KEY keyword. You also need to get rid of the quotes around the field names because dbDelta() chokes on those. Don't use backticks neither.

And, of course, set WP_DEBUG to TRUE while debugging.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.