Jake Vanderwerf
2026-01-05 9f86429a1252b45c95b7c62fbaa1b82de3723997
inc/registry/CheckCustomTables.php
@@ -44,7 +44,25 @@
      $this->userIDType = $this->getColumnType($this->userTable, 'ID');
      $this->termIDType = $this->getColumnType($this->wpdb->terms, 'term_id');
      $this->postIDType = $this->getColumnType($this->wpdb->posts, 'ID');
      error_log("JVB DEBUG: userTable = " . $this->userTable);
      error_log("JVB DEBUG: Users table exists = " . ($this->wpdb->get_var("SHOW TABLES LIKE '{$this->userTable}'") ? 'yes' : 'no'));
      error_log("JVB DEBUG: Users engine = " . $this->wpdb->get_var("SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{$this->userTable}'"));
      error_log("JVB DEBUG: MySQL version = " . $this->wpdb->db_version());
      $usersStatus = $this->wpdb->get_row("SHOW TABLE STATUS LIKE '{$this->userTable}'");
      $termsStatus = $this->wpdb->get_row("SHOW TABLE STATUS LIKE '{$this->wpdb->terms}'");
      error_log("JVB DEBUG: wp_users collation = " . $usersStatus->Collation);
      error_log("JVB DEBUG: wp_terms collation = " . $termsStatus->Collation);
      error_log("JVB DEBUG: Our charset_collate = " . $this->wpdb->get_charset_collate());
      error_log("JVB FK Types: users.ID={$this->userIDType}, terms.term_id={$this->termIDType}, posts.ID={$this->postIDType}");
      $usersDb = $this->wpdb->get_var(
         "SELECT TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{$this->userTable}' LIMIT 1"
      );
      error_log("JVB DEBUG: wp_users is in database = " . $usersDb);
      error_log("JVB DEBUG: Current DB_NAME = " . DB_NAME);
      error_log("JVB DEBUG: Tables being created in = " . $this->wpdb->dbname);
   }
   protected function getMultisiteUsersTable():string
@@ -368,73 +386,70 @@
      }
   }
    public function createTables(array $tables)
    {
        $charset_collate = $this->wpdb->get_charset_collate();
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
   public function createTables(array $tables)
   {
      require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        $errors = [];
        $created = [];
      // Match collation from existing WP tables for FK compatibility
      $usersStatus = $this->wpdb->get_row("SHOW TABLE STATUS LIKE '{$this->wpdb->users}'");
      $parentCollation = $usersStatus->Collation ?? 'utf8mb4_general_ci';
      $charset_collate = "DEFAULT CHARACTER SET utf8mb4 COLLATE {$parentCollation}";
        foreach ($tables as $name => $schema) {
            $table_name = $this->prefix . BASE . $name;
            $full_schema = "CREATE TABLE IF NOT EXISTS {$table_name} {$schema} {$charset_collate};";
      error_log("JVB: Using charset_collate: " . $charset_collate);
            // Clear any previous errors
            $this->wpdb->flush();
      $errors = [];
      $created = [];
            try {
                $result = dbDelta($full_schema);
      foreach ($tables as $name => $schema) {
         $table_name = $this->prefix . BASE . $name;
                // Check for SQL errors
                if ($this->wpdb->last_error) {
                    $error_msg = "SQL Error creating table {$table_name}: " . $this->wpdb->last_error;
                    error_log($error_msg);
                    error_log("Failed SQL Query: " . $full_schema);
                    $errors[] = $error_msg;
         // Skip if exists
         if ($this->wpdb->get_var("SHOW TABLES LIKE '{$table_name}'")) {
            $created[] = $table_name . ' (exists)';
            continue;
         }
                    // Don't throw exception, just log and continue
                    continue;
                }
         $full_schema = "CREATE TABLE IF NOT EXISTS {$table_name} {$schema} {$charset_collate}";
                // Verify table was actually created
                $table_exists = $this->wpdb->get_var("SHOW TABLES LIKE '{$table_name}'");
                if ($table_exists) {
                    $created[] = $table_name;
                    error_log("Successfully created table: {$table_name}");
                } else {
                    $error_msg = "Table {$table_name} was not created (no error reported)";
                    error_log($error_msg);
                    error_log("Schema used: " . $full_schema);
                    $errors[] = $error_msg;
                }
         $this->wpdb->flush();
            } catch (Exception $e) {
                $error_msg = "Exception creating table {$table_name}: " . $e->getMessage();
                error_log($error_msg);
                error_log("Exception SQL Query: " . $full_schema);
                $errors[] = $error_msg;
            }
        }
         // Use direct query - dbDelta mangles FK constraints
         $hasForeignKey = stripos($schema, 'FOREIGN KEY') !== false;
        // Log summary
        if (!empty($created)) {
            error_log("JVB Tables Created Successfully: " . implode(', ', $created));
        }
         if ($hasForeignKey) {
            $result = $this->wpdb->query($full_schema);
            $success = ($result !== false && !$this->wpdb->last_error);
         } else {
            dbDelta($full_schema . ';');
            $success = !$this->wpdb->last_error;
         }
        if (!empty($errors)) {
            error_log("JVB Table Creation Errors (" . count($errors) . " total):");
            foreach ($errors as $error) {
                error_log("  - " . $error);
            }
         if (!$success) {
            $error_msg = "SQL Error creating table {$table_name}: " . $this->wpdb->last_error;
            error_log($error_msg);
            error_log("Failed SQL Query: " . $full_schema);
            $errors[] = $error_msg;
         } elseif ($this->wpdb->get_var("SHOW TABLES LIKE '{$table_name}'")) {
            $created[] = $table_name;
            error_log("Successfully created table: {$table_name}");
         }
      }
            // Optionally store errors for admin display
            update_option(BASE . 'table_creation_errors', $errors);
        } else {
            // Clear any previous errors
            delete_option(BASE . 'table_creation_errors');
        }
    }
      // Log summary
      if (!empty($created)) {
         error_log("JVB Tables Created Successfully: " . implode(', ', $created));
      }
      if (!empty($errors)) {
         error_log("JVB Table Creation Errors (" . count($errors) . " total):");
         foreach ($errors as $error) {
            error_log("  - " . $error);
         }
         update_option(BASE . 'table_creation_errors', $errors);
      } else {
         delete_option(BASE . 'table_creation_errors');
      }
   }
    /******************************************************
     * Table Definitions
@@ -575,9 +590,9 @@
            KEY `requires_action` (`owner_id`, `requires_action`, `action_taken`),
            KEY `acting_user_lookup` (`owner_id`, `action_user_id`, `type`, `status`, `created_at`),
            CONSTRAINT `{$this->base}notify_owner` FOREIGN KEY (`owner_id`)
            REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
            REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}action_id` FOREIGN KEY (`action_user_id`)
            REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
            REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )",
@@ -617,7 +632,7 @@
            UNIQUE KEY `user_content_notif` (`user_id`, `content_notification_id`),
            KEY `user_status` (`user_id`, `status`),
            CONSTRAINT `{$this->base}user_content_user` FOREIGN KEY (`user_id`)
            REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
            REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}user_content_notification` FOREIGN KEY (`content_notification_id`)
            REFERENCES `{$this->prefixed}notifications_content` (`id`) ON DELETE CASCADE
        )",
@@ -637,7 +652,7 @@
            KEY `user_frequency` (`user_id`, `frequency`),
            KEY `frequency_lookup` (`frequency`, `last_sent`),
            CONSTRAINT `{$this->base}notification_pref_user` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )",
            // Notification digest scheduling and tracking
@@ -654,7 +669,7 @@
            KEY `scheduled_digests` (`frequency`, `scheduled_at`, `status`),
            KEY `user_digests` (`user_id`, `frequency`),
            CONSTRAINT `{$this->base}digest_user` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )",
            // Analytics on notification interactions
@@ -671,9 +686,9 @@
            KEY `user_actions` (`user_id`, `action`),
            KEY `action_analysis` (`action`, `action_source`),
            CONSTRAINT `{$this->base}metrics_user` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}metrics_notification` FOREIGN KEY (`notification_id`)
                REFERENCES {$this->prefixed}notifications (`id`) ON DELETE CASCADE
                REFERENCES `{$this->prefixed}notifications` (`id`) ON DELETE CASCADE
        )"
        ];
    }
@@ -702,7 +717,7 @@
                KEY `status` (`status`),
                KEY `expiring_requests` (`status`, `expires_at`),
                CONSTRAINT `{$this->base}{$type}_approval_requester` FOREIGN KEY (`user_id`)
                    REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                    REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
            )";
            $tables['approval_'.$type.'_votes'] = "(
                `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -715,9 +730,9 @@
                UNIQUE KEY `unique_vote` (`request_id`, `user_id`),
                KEY `user_votes` (`user_id`),
                CONSTRAINT `{$this->base}{$type}_user_approval_request` FOREIGN KEY (`request_id`)
                    REFERENCES {$this->prefixed}approval_{$type}_requests (`id`) ON DELETE CASCADE,
                    REFERENCES `{$this->prefixed}approval_{$type}_requests` (`id`) ON DELETE CASCADE,
                CONSTRAINT `{$this->base}{$type}_user_approval_voter` FOREIGN KEY (`user_id`)
                    REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                    REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
            )";
        }
        if (!empty($save)) {
@@ -747,9 +762,9 @@
      KEY `related_taxonomy` (`related_taxonomy`),
      UNIQUE KEY `term_relation` (`term_id`, `related_term_id`, `taxonomy`, `related_taxonomy`),
      CONSTRAINT `{$this->base}tax_rel_term_id` FOREIGN KEY (`term_id`)
         REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE,
         REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE,
      CONSTRAINT `{$this->base}tax_rel_related_id` FOREIGN KEY (`related_term_id`)
         REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE
         REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE
   )"
      ];
@@ -767,9 +782,9 @@
      KEY `user_taxonomy` (`user_id`, `taxonomy`),
      KEY `taxonomy` (`taxonomy`),
      CONSTRAINT `{$this->base}user_term_user_fk` FOREIGN KEY (`user_id`)
         REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
         REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
      CONSTRAINT `{$this->base}user_term_term_fk` FOREIGN KEY (`term_id`)
         REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE
         REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE
   )";
      }
@@ -792,7 +807,7 @@
            KEY `user_type` (`user_id`, `type`),
            KEY `target_type` (`target_id`, `type`),
            CONSTRAINT `{$this->base}favourites_user` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )",
            'favourites_lists'  => "(
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -804,7 +819,7 @@
            PRIMARY KEY (`id`),
            KEY `user_lists` (`user_id`),
            CONSTRAINT `{$this->base}list_user` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )",
            'favourites_list_items' => "(
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -818,9 +833,9 @@
            KEY `list_items` (`list_id`),
            KEY `favourite_id` (`favourite_id`),
            CONSTRAINT `{$this->base}list_items` FOREIGN KEY (`list_id`)
                REFERENCES {$this->prefixed}favourites_lists (`id`) ON DELETE CASCADE,
                REFERENCES    `{$this->prefixed}favourites_lists` (`id`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}list_favourite` FOREIGN KEY (`favourite_id`)
                REFERENCES {$this->prefixed}favourites (`id`) ON DELETE SET NULL
                REFERENCES `{$this->prefixed}favourites` (`id`) ON DELETE SET NULL
        )",
            'favourites_list_shares' => "(
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -838,9 +853,9 @@
            KEY `list_shares` (`list_id`),
            KEY `status_index` (`status`),
            CONSTRAINT `{$this->base}share_list` FOREIGN KEY (`list_id`)
                REFERENCES {$this->prefixed}favourites_lists (`id`) ON DELETE CASCADE,
                REFERENCES `{$this->prefixed}favourites_lists` (`id`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}share_user` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )",
            'favourites_list_stats'    => "(
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -872,11 +887,11 @@
                KEY `user_id` (`user_id`),
                KEY `artist_id` (`artist_id`),
            CONSTRAINT `{$this->base}nr_shop_news_shop` FOREIGN KEY (`shop_id`)
                REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE,
                REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}nr_shop_news_user` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}nr_shop_news_artist` FOREIGN KEY (`artist_id`)
                REFERENCES {$this->wpdb->posts} (`ID`) ON DELETE SET NULL
                REFERENCES `{$this->wpdb->posts}` (`ID`) ON DELETE SET NULL
        )"
        ];
    }
@@ -904,9 +919,9 @@
            KEY `parent_child` (`parent_id`),
            KEY `karma_order` (`karma`),
            CONSTRAINT `{$this->base}re_responses_news` FOREIGN KEY (`item_id`)
                REFERENCES {$this->wpdb->posts} (`ID`) ON DELETE CASCADE,
                REFERENCES `{$this->wpdb->posts}` (`ID`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}re_responses_parent` FOREIGN KEY (`parent_id`)
                REFERENCES {$this->prefixed}responses (`id`) ON DELETE SET NULL
                REFERENCES `{$this->prefixed}responses` (`id`) ON DELETE SET NULL
        )",
            'karma_response' => "(
                `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -919,9 +934,9 @@
                KEY `item_id` (`item_id`),
                KEY `user_id` (`user_id`),
            CONSTRAINT `{$this->base}_response_item_id` FOREIGN KEY (`item_id`)
                REFERENCES {$this->prefixed}responses (`id`) ON DELETE CASCADE,
                REFERENCES `{$this->prefixed}responses` (`id`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}_response_user_id` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
                )"
        ];
    }
@@ -972,9 +987,9 @@
                KEY `item_id` (`item_id`),
                KEY `user_id` (`user_id`),
                CONSTRAINT `{$this->base}kt_{$type}_item_id` FOREIGN KEY (`item_id`)
                    REFERENCES {$reference_table} ({$reference_column}) ON DELETE CASCADE,
                    REFERENCES `{$reference_table}` (`{$reference_column}`) ON DELETE CASCADE,
                CONSTRAINT `{$this->base}kt_{$type}_user_id` FOREIGN KEY (`user_id`)
                    REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                    REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
            )";
        }
@@ -1051,13 +1066,13 @@
                KEY `rsvp_events` (`rsvp_required`, `rsvp_deadline`),
                CONSTRAINT `{$this->base}cal_{$type}_type` FOREIGN KEY (`event_type`)
                    REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE,
                    REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE,
                CONSTRAINT `{$this->base}cal_{$type}_post` FOREIGN KEY (`post_id`)
                    REFERENCES {$this->wpdb->posts} (`ID`) ON DELETE CASCADE,
                    REFERENCES `{$this->wpdb->posts}` (`ID`) ON DELETE CASCADE,
                CONSTRAINT `{$this->base}cal_{$type}_shop` FOREIGN KEY (`shop_id`)
                    REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE SET NULL,
                    REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE SET NULL,
                CONSTRAINT `{$this->base}cal_{$type}_user` FOREIGN KEY (`user_id`)
                    REFERENCES {$this->userTable} (`ID`) ON DELETE SET NULL
                    REFERENCES `{$this->userTable}` (`ID`) ON DELETE SET NULL
            )";
            $tables['calendar_'.$type.'_participants'] = "(
@@ -1069,9 +1084,9 @@
                PRIMARY KEY (`id`),
                UNIQUE KEY `event_user` (`event_id`, `user_id`),
                CONSTRAINT `{$this->base}cal_{$type}_participant_event` FOREIGN KEY (`event_id`)
                    REFERENCES {$this->prefixed}calendar_{$type} (`id`) ON DELETE CASCADE,
                    REFERENCES `{$this->prefixed}calendar_{$type}` (`id`) ON DELETE CASCADE,
                CONSTRAINT `{$this->base}cal_{$type}_participant_user` FOREIGN KEY (`user_id`)
                    REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                    REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
            )";
            $tables['calendar_'.$type.'_recurrence_exceptions'] = "(
@@ -1117,9 +1132,9 @@
            KEY `user_idx` (`user_id`),
            KEY `owner_idx` (`owner_id`),
            CONSTRAINT `{$this->base}umami_user_id_link` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
            CONSTRAINT `{$this->base}umami_owner_id_link` FOREIGN KEY (`owner_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )",
            'stats_performance' => "(
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
@@ -1137,7 +1152,7 @@
            PRIMARY KEY (`id`),
            KEY `user_date_idx` (`user_id`, `date`),
            CONSTRAINT `{$this->base}performance_user_id_link` FOREIGN KEY (`user_id`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
        )"
        ];
    }
@@ -1170,7 +1185,7 @@
            )";
         foreach($config['to_terms']??[] as $term) {
            $definitions .= "CONSTRAINT `{$this->base}_{$term}_link` FOREIGN KEY (`to_{$term}`)
                REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE";
                REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE";
         }
         $tables['invitations_'.$role] = $definitions;
@@ -1199,9 +1214,9 @@
                    UNIQUE KEY `content_term` (`content_id`, `term_id`),
                    KEY content_role (`term_id`, `role`),
                    CONSTRAINT `{$this->base}{$content}_{$type}_history_user` FOREIGN KEY (`user_id`)
                        REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
                        REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
                    CONSTRAINT `{$this->base}{$content}_{$type}_history_term` FOREIGN KEY (`term_id`)
                        REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE
                        REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE
                )";
            }
        }
@@ -1228,9 +1243,9 @@
                    PRIMARY KEY (`id`),
                    UNIQUE KEY `{$this->base}content_term` (`content_id`, `term_id`),
                    CONSTRAINT `{$this->base}{$content}_{$type}_request_user` FOREIGN KEY (`user_id`)
                        REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
                        REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
                    CONSTRAINT `{$this->base}{$content}_{$type}_request_term` FOREIGN KEY (`term_id`)
                        REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE
                        REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE
                )";
            }
        }
@@ -1271,9 +1286,9 @@
   KEY `date_idx` (`referred_at`),
   KEY `consult_idx` (`consulted_at`),
   CONSTRAINT `{$this->base}referral_referrer_fk` FOREIGN KEY (`referrer_id`)
      REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE,
      REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE,
   CONSTRAINT `{$this->base}referral_referee_fk` FOREIGN KEY (`referee_id`)
      REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
      REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
)";
      // Create the main referrals table first
@@ -1297,7 +1312,7 @@
   KEY `user_idx` (`user_id`),
   KEY `email_idx` (`email`),
   CONSTRAINT `{$this->base}jane_client_user_fk` FOREIGN KEY (`user_id`)
      REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
      REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
)";
      // Third: referral_treatments (depends on referrals AND wp_users)
@@ -1317,9 +1332,9 @@
   KEY `date_idx` (`treatment_date`),
   KEY `type_idx` (`treatment_type`),
   CONSTRAINT `{$this->base}treatment_referral_fk` FOREIGN KEY (`referral_id`)
      REFERENCES {$this->prefixed}referrals (`id`) ON DELETE CASCADE,
      REFERENCES `{$this->prefixed}referrals` (`id`) ON DELETE CASCADE,
   CONSTRAINT `{$this->base}treatment_user_fk` FOREIGN KEY (`user_id`)
      REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
      REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
)";
      // Fourth: referral_rewards (depends on referrals AND wp_users)
@@ -1341,9 +1356,9 @@
   KEY `status_idx` (`status`),
   KEY `type_idx` (`reward_type`),
   CONSTRAINT `{$this->base}reward_referral_fk` FOREIGN KEY (`referral_id`)
      REFERENCES {$this->prefixed}referrals (`id`) ON DELETE CASCADE,
      REFERENCES `{$this->prefixed}referrals` (`id`) ON DELETE CASCADE,
   CONSTRAINT `{$this->base}reward_user_fk` FOREIGN KEY (`user_id`)
      REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE
      REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE
)";
      return $dependentTables;
@@ -1465,13 +1480,13 @@
         if ($type === 'taxonomy') {
            $constraints[] = "CONSTRAINT `{$constraintName}` FOREIGN KEY (`{$fieldName}`)
                REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE SET NULL";
                REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE SET NULL";
         } elseif ($type === 'user') {
            $constraints[] = "CONSTRAINT `{$constraintName}` FOREIGN KEY (`{$fieldName}`)
                REFERENCES {$this->userTable} (`ID`) ON DELETE SET NULL";
                REFERENCES `{$this->userTable}` (`ID`) ON DELETE SET NULL";
         } elseif ($type === 'image' || $type === 'file') {
            $constraints[] = "CONSTRAINT `{$constraintName}` FOREIGN KEY (`{$fieldName}`)
                REFERENCES {$this->wpdb->posts} (`ID`) ON DELETE SET NULL";
                REFERENCES `{$this->wpdb->posts}` (`ID`) ON DELETE SET NULL";
         }
      }
@@ -1568,7 +1583,7 @@
            ["PRIMARY KEY (`term_id`)"],
            $indexes,
            $constraints,
            ["CONSTRAINT `{$base}_{$type}_term` FOREIGN KEY (`term_id`) REFERENCES {$this->wpdb->terms} (`term_id`) ON DELETE CASCADE"]
            ["CONSTRAINT `{$base}_{$type}_term` FOREIGN KEY (`term_id`) REFERENCES `{$this->wpdb->terms}` (`term_id`) ON DELETE CASCADE"]
         );
         $sql = "(\n    " . implode(",\n    ", $allDefinitions) . "\n)";
         error_log("JVB: Creating content table for type: {$type}");
@@ -1732,7 +1747,7 @@
        $constraints = [
            "CONSTRAINT `{$this->base}_{$userType}_stats_user` FOREIGN KEY (`user_id`)
         REFERENCES {$this->userTable} (`ID`) ON DELETE CASCADE"
         REFERENCES `{$this->userTable}` (`ID`) ON DELETE CASCADE"
        ];
        $allDefinitions = array_merge($columns, $indexes, $constraints);