Site icon Hip-Hop Website Design and Development

How to create multiple database tables on plugin activation?

I have an activation hook to create 2 new database tables that don’t exist, only the second table is created from this code:

    public function add_tables() {

        // Global $wpdb

        global $wpdb;
        $wpdb->hide_errors();

        // Require upgrade

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

        // Set charset

        $collate = '';
        if ( $wpdb->has_cap( 'collation' ) ) {
            $collate = $wpdb->get_charset_collate();
        }

        // SQL query

        $sql = "
        CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "test1 (
            test_id bigint(20) NOT NULL AUTO_INCREMENT,
            test_key char(64) NOT NULL,
        ) $collate;
        CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "test2 (
            test_id bigint(20) NOT NULL AUTO_INCREMENT,
            test_key char(64) NOT NULL,
        ) $collate;
        ";

        // Do SQL

        dbDelta( $sql );

    }

Why only the second? If I print out the $sql variable I get the SQL statement and if I run that in phpMyAdmin it creates the 2 tables.

I looked at how plugins like WooCommerce do it (https://github.com/woocommerce/woocommerce/blob/c04f7b79f972ee854e5f5d726eb78ac04a726b32/includes/class-wc-install.php#L687) and it appears I am doing the same as they are.