# sqlalchemy.exc.ArgumentError：映射器 Mapper|User|users 无法为映射表 'users' 组装任何主键列

- **ID:** `python/sqlalchemy-missing-primary-key`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

模型类缺少主键列定义。

## 版本兼容性

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

## 解决方案

1. **Add a primary key column.** (95% 成功率)
   ```
   class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
   ```
2. **Use __mapper_args__ to specify primary key from existing columns.** (90% 成功率)
   ```
   class User(Base):
    __tablename__ = 'users'
    email = Column(String, primary_key=True)  # if email is unique
   ```

## 无效尝试

- **Adding a column without primary_key=True.** — SQLAlchemy requires explicit primary key constraint. (80% 失败率)
- **Using composite primary key with non-unique columns.** — Must ensure uniqueness; otherwise mapping fails. (60% 失败率)
