Error of "headers already sent" when creating tables in the activation of plugin

0

I am developing a plugin for WordPress, in which during activation, two tables that relate must be created. My code looks like this:

function create_tables(){
    global $wpdb;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    // Query para a criação da tabela de parceiros.
    $sql_partners = "CREATE TABLE ".$wpdb->prefix."partners(
                    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    name VARCHAR(355) NOT NULL,
                    phone VARCHAR(12),
                    address VARCHAR(355) NOT NULL
                    );";

    dbDelta($sql_partners);

    // Query para a criação da tabela de categorias. Parceiros são agrupados por categoria.
    $sql_categories = "CREATE TABLE ".$wpdb->prefix."partner_categories(
                      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                      name VARCHAR(100) NOT NULL,
                      FOREIGN_KEY (partner_id) references partners(id)
                      );";

    dbDelta($sql_categories);
}

This function is already properly configured in register_activation_hook of the plugin. The first table is created. But the second, it seems, is simply ignored and I get the following error on the activation screen:

  

This plugin generated 875 characters of unexpected output during activation. If you find messages from "headers already sent" problems with feeds or others, try to disable or remove the plugin.

I've tried several things, including changing the file's encoding. It simply does not work. Has anyone had this and could you tell me what's wrong there?

    
asked by anonymous 08.11.2014 / 18:42

1 answer

2

In addition, you have set FOREIGN_KEY (partner_id) and should be id of the same table, that is, FOREIGN_KEY (id) . And as colleagues said, you forgot the prefix when doing FOREIGN KEY . Try this:

function create_tables(){
    global $wpdb;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    // Query para a criação da tabela de parceiros.
    $sql_partners = "CREATE TABLE ".$wpdb->prefix."partners(
                    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                    name VARCHAR(355) NOT NULL,
                    phone VARCHAR(12),
                    address VARCHAR(355) NOT NULL
                    );";

    dbDelta($sql_partners);

    // Query para a criação da tabela de categorias. Parceiros são agrupados por categoria.
    $sql_categories = "CREATE TABLE ".$wpdb->prefix."partner_categories(
                      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                      name VARCHAR(100) NOT NULL,
                      FOREIGN_KEY (id) references ".$wpdb->prefix."partners(id)
                      );";

    dbDelta($sql_categories);
}
    
09.11.2014 / 22:04