4

I wrote a Plugin, which creates a table on activation. On previous WP Versions, it worked fine.

Now it doesn't. This is my function:

function on_activation(){
            global $wpdb;
            $table_name = $wpdb->prefix."tc_competition_data_all_countries_and_fields";
            $sql = "CREATE TABLE $table_name (
              mail VARCHAR(55),
              id VARCHAR(255) NOT NULL,
              time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
              name VARCHAR(255) NOT NULL,
              stra VARCHAR(255) NOT NULL,
              ort VARCHAR(255) NOT NULL,
              uid VARCHAR(255) NOT NULL,
              msg VARCHAR(500) NOT NULL,
              meta1 VARCHAR(500) NOT NULL,
              meta2 VARCHAR(500) NOT NULL,
              UNIQUE KEY id (id)
            );";
            #dbDelta( $sql );
            $c  = dbDelta( $sql );
        }

It gets fied with this hook:

register_activation_hook( __FILE__, array( 'tc_comp_form', 'on_activation' ) );

When I do a vardump in for $c, it returns, that the table has been created, but it does not exists.

What could be the issue?

Many Thanks!

2
  • Your table should have a primary key, also dbdelta is very fussy about the way the SQL is formatted
    – Tom J Nowell
    Commented Nov 15, 2016 at 17:12
  • This code worked for me. I had to add require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); prior to calling dbdelta($sql), but I'm figuring that was omitted in the question by accident? mysql v10.1.10-MariaDB Commented Nov 15, 2016 at 17:39

1 Answer 1

4

The Problem was in this line:

time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,

I changed it to:

time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

Now, it works.

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.