# android.database.sqlite.SQLiteConstraintException: NOT NULL 约束失败: users.id (代码 1555)

- **ID:** `android/room-insert-null-primary-key`
- **领域:** android
- **类别:** data_error
- **错误码:** `1555`
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

Room 实体具有非空主键字段且非自动生成，但插入时提供了空值。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Room 2.6.1 | active | — | — |
| SQLite 3.44.0 | active | — | — |
| Android 14 (API 34) | active | — | — |

## 解决方案

1. ```
   Add @PrimaryKey(autoGenerate = true) to the id field. Example: @PrimaryKey(autoGenerate = true) val id: Long = 0. This auto-generates unique IDs.
   ```
2. ```
   If auto-generate not desired, ensure you assign a non-null value before insert: user.id = System.currentTimeMillis() or use UUID.randomUUID().toString() for string keys.
   ```

## 无效尝试

- **Set primary key field to nullable in entity class** — Room does not allow nullable primary keys; will cause compile error or runtime crash. (95% 失败率)
- **Use @Ignore on primary key field to skip Room processing** — Ignores the field entirely, but then no primary key exists, breaking database operations. (90% 失败率)
