# Production Fix Guide: "Field 'permission_id' doesn't have a default value" Error

## Problem
Your production server on cPanel is experiencing the error: `(1364, "Field 'permission_id' doesn't have a default value")` when trying to update permissions.

## Root Cause
The `users_user_permissions` table is either missing or has incorrect column sizes that can't accommodate Django's UUID format.

## Solution Options

### Option 1: Run Python Script (Recommended)
1. Upload `fix_production_permissions.py` to your production server
2. SSH into your server or use cPanel's Terminal
3. Navigate to your Django project directory
4. Run: `python fix_production_permissions.py`

### Option 2: Run SQL Script in phpMyAdmin
1. Open cPanel → phpMyAdmin
2. Select your database
3. Go to the SQL tab
4. Copy and paste the contents of `fix_production_permissions.sql`
5. Click "Go" to execute

### Option 3: Manual Database Fix
If you prefer to do it manually:

1. **Check current state:**
   ```sql
   SHOW TABLES LIKE 'users%';
   DESCRIBE users_user_permissions;
   ```

2. **Drop existing tables (if they exist):**
   ```sql
   DROP TABLE IF EXISTS users_user_permissions;
   DROP TABLE IF EXISTS users_groups;
   ```

3. **Create the correct tables:**
   ```sql
   CREATE TABLE users_user_permissions (
       id INT AUTO_INCREMENT PRIMARY KEY,
       customuser_id CHAR(36) NOT NULL,
       permission_id INT NOT NULL,
       UNIQUE KEY users_user_permissions_customuser_id_permission_id_uniq (customuser_id, permission_id),
       KEY users_user_permissions_customuser_id (customuser_id),
       KEY users_user_permissions_permission_id (permission_id),
       CONSTRAINT users_user_permissions_customuser_id_fk 
           FOREIGN KEY (customuser_id) REFERENCES users (id) ON DELETE CASCADE,
       CONSTRAINT users_user_permissions_permission_id_fk 
           FOREIGN KEY (permission_id) REFERENCES auth_permission (id) ON DELETE CASCADE
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

   CREATE TABLE users_groups (
       id INT AUTO_INCREMENT PRIMARY KEY,
       customuser_id CHAR(36) NOT NULL,
       group_id INT NOT NULL,
       UNIQUE KEY users_groups_customuser_id_group_id_uniq (customuser_id, group_id),
       KEY users_groups_customuser_id (customuser_id),
       KEY users_groups_group_id (group_id),
       CONSTRAINT users_groups_customuser_id_fk 
           FOREIGN KEY (customuser_id) REFERENCES users (id) ON DELETE CASCADE,
       CONSTRAINT users_groups_group_id_fk 
           FOREIGN KEY (group_id) REFERENCES auth_group (id) ON DELETE CASCADE
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
   ```

## Verification
After running the fix, test that permissions work:

1. Try to assign a permission to a user in your Django admin
2. Check that no errors occur
3. Verify that the permission is actually assigned

## Important Notes

⚠️ **WARNING**: The SQL script will drop and recreate the tables, which will delete any existing user permission data. If you have important permission data, back it up first.

✅ **Safe**: The Python script is safer as it checks the existing structure and only modifies what's necessary.

## Files Included
- `fix_production_permissions.py` - Python script for automated fix
- `fix_production_permissions.sql` - SQL script for manual fix
- `PRODUCTION_FIX_GUIDE.md` - This guide

## After the Fix
Once the fix is applied, your Django application should be able to:
- Assign permissions to users
- Remove permissions from users
- Manage user groups
- All without the "Field 'permission_id' doesn't have a default value" error

The permission system will be fully functional in production.
