# pydantic_core._pydantic_core.ValidationError: 用户模型验证错误：name 字段缺失

- **ID:** `python/pydantic-validation-error-field-required`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

实例化 Pydantic 模型时输入数据缺少必填字段。在 Pydantic v2 中，缺失字段会抛出 ValidationError，而不是静默使用默认值。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   from typing import Optional
from pydantic import BaseModel

class User(BaseModel):
    name: Optional[str] = None
    age: int
   ```
2. **** (98% 成功率)
   ```
   user = User(name='Alice', age=25)  # include all required fields
# or
User.model_validate({'name': 'Alice', 'age': 25})
   ```

## 无效尝试

- **** — Silently swallowing ValidationError means downstream code receives an object with missing attributes, causing AttributeError later. (85% 失败率)
- **** — Pydantic v2 models are immutable by default and reject attribute assignment unless model_config allows it. (70% 失败率)
