# ERROR 1781 (HY000): @_NEXT cannot be set to UUID:NUMBER when @_MODE = ON.

- **ID:** `database/mysql-gtid-purge-failure`
- **Domain:** database
- **Category:** runtime_error
- **Error Code:** `1781`
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

Attempting to manually set gtid_next to a specific GTID while GTID mode is enabled, which is not allowed because GTIDs are automatically generated by the server.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MySQL 5.7 | active | — | — |
| MySQL 8.0 | active | — | — |
| MySQL 8.4 | active | — | — |

## Workarounds

1. **Set gtid_next to 'AUTOMATIC' first: `SET @@SESSION.GTID_NEXT = 'AUTOMATIC';` Then let the server generate the GTID automatically. If you need to inject a specific GTID (e.g., for replication recovery), use `SET GTID_NEXT = 'UUID:NUMBER'` only after ensuring GTID_MODE is OFF or using a different session.** (95% success)
   ```
   Set gtid_next to 'AUTOMATIC' first: `SET @@SESSION.GTID_NEXT = 'AUTOMATIC';` Then let the server generate the GTID automatically. If you need to inject a specific GTID (e.g., for replication recovery), use `SET GTID_NEXT = 'UUID:NUMBER'` only after ensuring GTID_MODE is OFF or using a different session.
   ```
2. **If you need to skip a GTID (e.g., after a duplicate key error on replica), use `SET GTID_NEXT = 'CONSISTENCY';` followed by `BEGIN; COMMIT;` and then reset to AUTOMATIC: `SET @@SESSION.GTID_NEXT = 'AUTOMATIC';`** (90% success)
   ```
   If you need to skip a GTID (e.g., after a duplicate key error on replica), use `SET GTID_NEXT = 'CONSISTENCY';` followed by `BEGIN; COMMIT;` and then reset to AUTOMATIC: `SET @@SESSION.GTID_NEXT = 'AUTOMATIC';`
   ```

## Dead Ends

- **Set gtid_next to 'AUTOMATIC' before setting it to a specific value** — This is the default state; the error occurs because you're trying to set gtid_next to a specific UUID:NUMBER, which is only allowed in manual mode or when GTID_MODE is OFF (90% fail)
- **Disable GTID mode globally with SET GLOBAL gtid_mode = OFF** — Changing gtid_mode requires a specific sequence (OFF -> OFF_PERMISSIVE -> ON_PERMISSIVE -> ON) and may break replication if GTIDs are in use (70% fail)
- **Ignore the error and proceed with the transaction** — The transaction will fail to commit because gtid_next is in an invalid state (100% fail)
