WordPress.org

Ready to get started?Download WordPress

Forums

dbDelta: Issues with upgrading tables with composite primary key (3 posts)

  1. yobikwhatus
    Member
    Posted 2 years ago #

    I typically don't have any issues with dbDelta. However, I came across a problem during upgrade that has to do with using a composite primary key. The first time the below code is run, the table is deployed with no errors. However, the moment I run deployment again with dbDelta, it bombs out. I'm currently on 3.3.1.

    Code:
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    function yobik_whatupwiththis () {
    	global $wpdb;
    
    	$sql[] = "CREATE TABLE my_table (
    		yo_id int(11) NOT NULL default 0,
    		bik_id smallint(6) NOT NULL default 0,
    		wha_id smallint(6) NOT NULL default 0,
    		tus_id smallint(6) NOT NULL default 0,
    		PRIMARY KEY (yo_id, bik_id))";
    
    	dbDelta($sql);
    }

    Resulting Error:
    WordPress database error: [Multiple primary key defined]
    ALTER TABLE my_table ADD PRIMARY KEY (yo_id, bik_id)

    Wondering if somebody has come across this issue.

    Thanks in advance.

  2. James Collins
    Member
    Posted 2 years ago #

    According to http://hungred.com/how-to/wordpress-dbdelta-function/, this may be failing because of the space in "yo_id, bik_id".

    Try replacing
    PRIMARY KEY (yo_id, bik_id))
    with
    PRIMARY KEY (yo_id,bik_id))

  3. yobikwhatus
    Member
    Posted 2 years ago #

    Whoa. Thanks for the heads-up.

Topic Closed

This topic has been closed to new replies.

About this Topic