# sqlalchemy.exc.NoSuchTableError: 表 'orders' 未找到

- **ID:** `python/sqlalchemy-missing-table-definition`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

ORM 模型引用了数据库中不存在的表，或元数据未正确加载。

## 版本兼容性

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

## 解决方案

1. **Use Base.metadata.create_all() to create missing tables** (95% 成功率)
   ```
   from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
Base.metadata.create_all(engine)
   ```
2. **Load table metadata from the database** (90% 成功率)
   ```
   from sqlalchemy import Table, MetaData
metadata = MetaData()
orders_table = Table('orders', metadata, autoload_with=engine)
   ```

## 无效尝试

- **Manually creating the table with raw SQL** — The ORM model may still fail if metadata is not refreshed. (65% 失败率)
- **Ignoring the error and proceeding** — Subsequent queries will fail with the same error. (100% 失败率)
