• I have an issue with dbDeltaif the table already exists. I expected that dbDelta would do nothing if the table is already created but it
    logs WordPress Table ‘wp_mis_cursos’ already exists

    register_activation_hook( __FILE__, 'mis_cursos_install' );  // 
    
    function mis_cursos_install() {   
       global $wpdb;
       global $mis_cursos_cursos_db;
       
       $table_name = $wpdb->prefix . "mis_cursos";
          
       $sql = "CREATE TABLE $table_name(
      id mediumint(9) NOT NULL AUTO_INCREMENT,
      codigo tinytext NOT NULL,
      nombre tinytext NOT NULL,
      texto text NOT NULL,
      precio DECIMAL(15,2) NOT NULL,
      nivel VARCHAR(55) DEFAULT '' NOT NULL,
      enventa tinytext NOT NULL,
      arriba tinytext NOT NULL,
      textoventa varchar(80) DEFAULT '' NOT NULL, 
      url VARCHAR(255) DEFAULT '' NOT NULL,
      urlpage VARCHAR(255) DEFAULT '' NOT NULL,
      urlimage VARCHAR(255) DEFAULT '' NOT NULL,
      ficheros VARCHAR(255) DEFAULT '' NOT NULL,
      eventos VARCHAR(255) DEFAULT '' NOT NULL,
      test VARCHAR(255) DEFAULT '' NOT NULL,
      videos VARCHAR(255) DEFAULT '' NOT NULL,
      certificate VARCHAR(255) DEFAULT '' NOT NULL,
      UNIQUE KEY id (id)
        );";
    
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
    
        // Guarda la versión
        update_option( "mis_cursos_cursos_db", $mis_cursos_cursos_db );
    }
    • This topic was modified 4 years, 3 months ago by bcworkz. Reason: code fixed
Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion

    (@diondesigns)

    You should use CREATE TABLE IF NOT EXISTS in your query.

    You might also consider adding indexes on columns that will be used to filter/order rows in a SELECT query.

    Thread Starter capbussat

    (@capbussat)

    You are right about the index. I have used the following (just for the record):
    ALTER TABLEwp_mis_cursosADD INDEXcodigo(codigo`(20));

    You are right also about using IF NOT EXISTS avoids logging and error, but it was not what I intended using dbDelta.

    For me, dbDelta should upgrade the table if-and-only-if there are changes at previous table structure, like adding or removing columns.
    My issue is with dbDelta itself, in this case I do not know why dbdelta does not work.

    Thanks

    • This reply was modified 4 years, 3 months ago by capbussat.
    • This reply was modified 4 years, 3 months ago by capbussat.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘dbDelta issue’ is closed to new replies.