# sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1366, "第 1 行的 'message' 列出现不正确的字符串值：'\xF0\x9F\x98\x80'")

- **ID:** `python/sqlalchemy-mysql-charset-issue`
- **领域:** python
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

数据库/表/列的字符集不是 utf8mb4，无法存储像表情符号这样的 4 字节 Unicode 字符。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.4.x | active | — | — |
| 2.0.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Use charset='utf8mb4' in the connection URL: engine = create_engine('mysql+pymysql://user:pass@host/db?charset=utf8mb4')
   ```
2. **** (85% 成功率)
   ```
   Alter the table to use utf8mb4: ALTER TABLE messages CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
   ```

## 无效尝试

- **** — utf8 in MySQL is actually utf8mb3, which still doesn't support 4-byte characters. (90% 失败率)
- **** — Corrupts the data. (80% 失败率)
