Skip to content

support ALTER TABLE AUTO_INCREMENT #35

@batonac

Description

@batonac

The query in this function (edit: example from a third-party plugin) produces an error (with the latest trunk):

public function reset_auto_increment_availability_table()
{
    global $wpdb;
    $tablename      = $wpdb->prefix.$this->availability_tablename;
    $row            = $wpdb->get_row( "SELECT Max(sno) as max_sno FROM $tablename" );
    
    $auto_increment = (int) $row->max_sno + 1;
    // error_log('auto_increment : '.print_r($auto_increment,1));
    $prepare_values = array( $auto_increment );
    $query 			= $wpdb->prepare( "ALTER TABLE $tablename AUTO_INCREMENT = %d", $prepare_values);
    $status         = $wpdb->query($query);
}

Here's the temporary workaround I used (thanks to ChatGPT):

public function reset_auto_increment_availability_table()
{
    global $wpdb;
    $tablename      = $wpdb->prefix.$this->availability_tablename;
    $row            = $wpdb->get_row( "SELECT Max(sno) as max_sno FROM $tablename" );

    $auto_increment = (int) $row->max_sno + 1;
    // error_log('auto_increment : '.print_r($auto_increment,1));
    $prepare_values = array( $auto_increment );
    
    // Check if the database is SQLite
    if ($wpdb instanceof WP_SQLite_DB) {
        $query = $wpdb->prepare( "UPDATE sqlite_sequence SET seq = %d WHERE name = %s", array($auto_increment, $tablename));
    } else {
        $query = $wpdb->prepare( "ALTER TABLE $tablename AUTO_INCREMENT = %d", $prepare_values);
    }

    $status = $wpdb->query($query);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions