1

I am creating a wordpress plugin in which I am creating a table, the table is getting generated successfully on activating the plugin but the problem is, column id is not generating as AUTO INCREMENT

public static function activate()
    {


    global $wpdb;

    $sql = "CREATE TABLE `{$wpdb->base_prefix}employee` (
    `id` int NOT NULL,
    `name` varchar(255) NOT NULL,
    `dob` date NOT NULL,
    `gender` int NOT NULL,
    `email` varchar(255) NOT NULL,
    `joining_date` date NOT NULL,
    `address` varchar(255) NOT NULL,
    `salary_package` int NOT NULL,
    `marital_status` int NOT NULL,
    `department` varchar(255) NOT NULL,
    `designation` varchar(255) NOT NULL,
    PRIMARY KEY (id)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci AUTO_INCREMENT=1;
  COMMIT;";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    
    }

1 Answer 1

2

Add the autoincrement keyword after `id` int NOT NULL:

public static function activate() {
    global $wpdb;

    $sql = "CREATE TABLE `{$wpdb->base_prefix}employee` (
    `id` int NOT NULL autoincrement,
    `name` varchar(255) NOT NULL,
    `dob` date NOT NULL,
    `gender` int NOT NULL,
    `email` varchar(255) NOT NULL,
    `joining_date` date NOT NULL,
    `address` varchar(255) NOT NULL,
    `salary_package` int NOT NULL,
    `marital_status` int NOT NULL,
    `department` varchar(255) NOT NULL,
    `designation` varchar(255) NOT NULL,
    PRIMARY KEY (id)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci AUTO_INCREMENT=1;
  COMMIT;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

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.