---
date: 2026-05-19
tags: [prisma, claude-code, destructive-ops, safety-guardrail, dev-env]
severity: medium
status: workaround-known
---

# Prisma 6 对 Claude Code 的 destructive op 护栏

## 现象

在 Claude Code session 里跑 `npm run db:reset -- --force`（即 `prisma migrate reset --force`），Prisma 6 直接拒绝执行并报：

```
Error: Prisma Migrate detected that it was invoked by Claude Code.

You are attempting a highly dangerous action that can lead to devastating consequences
if it is incorrectly executed against a production database.

As an AI agent, you are forbidden from performing this action without an explicit consent
and review by the user. ...

If they explicitly consent, you may rerun this command with PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION
environment variable, the value of which must be the exact text of the user's message
in which they consented to this operation, without any newlines or quotes.
```

CLI 标志 `--force` 不能绕过这个护栏。即使是 dev 数据库（端口非标准、库名带 slot 后缀）也照样拦截。

## 根因

Prisma 6.x schema-engine 检测到 Claude Code 调用方（推断方式应该是看父进程 / env），对 destructive op (`migrate reset` 等) 强制要求一个特殊 env var 携带用户原话同意，作为审计/不可绕过证据。和 CLAUDE.md 的"destructive 操作需要 user confirm"方向一致，但这是 Prisma 在工具层加的保险。

## 绕行方案（必须的正确做法）

1. AI 先停下来给 user 列：要执行的命令、目标库（确认是 dev 非 prod）、不可逆影响、原因
2. 等 user 明文同意（不要把之前的"继续"当 implicit consent，必须就这个操作明示）
3. 用 user 原话作为 token（**不带 newline 和 quotes**）：

```bash
PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION="<user 原话>" \
  npm run db:reset -- --force
```

## 适用范围

会触发护栏的命令（实测 + 推测）：
- `prisma migrate reset`（实测）
- 可能：`prisma db push --force-reset`、`prisma migrate dev` 触发 drop database 分支

不会触发的：
- `prisma db push`（无 `--force-reset`）
- `prisma db push --accept-data-loss`（数据丢失但不重建库）
- `prisma generate`
- `prisma migrate deploy`

## 关键点

- env var 名字很长易拼错：`PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION`
- 值是 user 原话，不是 yes/true/任意字符串
- 别试图绕过（比如 fake env var、改 prisma binary）—— 这条护栏是设计成 AI 不能自己解开的

## 跟 CLAUDE.md 的关系

CLAUDE.md「Git 安全 / 5a 生产环境只读」「Executing actions with care」要求 destructive op 前 user confirm。Prisma 的护栏是工具层兜底，方向一致，可以接受。

但 AI 跑流水准备 dev 环境时这条会反复触发——记住正确流程，别浪费 user 时间反复问。
