You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Python FastAPI项目至Vercel时无法生成Prisma Client

Vercel部署FastAPI+Prisma项目时Prisma Client生成失败的解决方案

问题场景

部署Vercel上的Python FastAPI项目时,尽管已通过pip install安装requirements.txt依赖,prisma generate步骤未正常执行,导致Prisma Client未生成,无法在应用中连接数据库。相关配置如下:

Prisma schema:

generator client {
  provider = "prisma-client-py"
}

datasource db {
  provider = "postgresql"
  url      = env("POSTGRES_URL_NON_POOLING")
}

已尝试操作:

  • 在vercel.json中配置包含prisma generate的构建命令
  • 确保prisma/(含schema和迁移文件)与api/main.py的目录结构正确
  • 本地.env已配置数据库URL

解决方案

1. 显式指定Prisma Schema路径执行生成

Vercel构建环境可能无法自动识别默认schema路径,修改vercel.json的构建命令,明确指定schema位置:

{
  "version": 2,
  "builds": [
    {
      "src": "api/main.py",
      "use": "@vercel/python",
      "config": {
        "buildCommand": "pip install -r requirements.txt && python -m prisma generate --schema ./prisma/schema.prisma",
        "runtime": "python3.10"
      }
    }
  ]
}

2. 配置依赖安装后自动生成客户端

在requirements.txt中添加Prisma的post-install参数,让依赖安装完成后自动触发客户端生成:

prisma==0.15.0
fastapi
# 其他依赖
--install-option="--post-install=prisma generate"

注:若此方法失效,可结合构建命令的显式调用一起使用

3. 固定构建工作目录

确保Vercel在项目根目录执行构建命令,避免路径识别错误,在vercel.json中添加workingDirectory配置:

{
  "version": 2,
  "builds": [
    {
      "src": "api/main.py",
      "use": "@vercel/python",
      "config": {
        "buildCommand": "pip install -r requirements.txt && python -m prisma generate",
        "runtime": "python3.10",
        "workingDirectory": "."
      }
    }
  ]
}

4. 冷启动时兜底生成(应急方案)

若构建阶段始终无法生成,可在FastAPI启动前手动触发生成,添加到api/main.py中:

from prisma import Prisma
import subprocess
import os
from fastapi import FastAPI

# 检查客户端是否存在,不存在则生成
if not os.path.exists('./prisma/client'):
    subprocess.run(['python', '-m', 'prisma', 'generate'], check=True)

app = FastAPI()

# 后续路由与数据库操作逻辑

注:此方法会增加冷启动耗时,仅作为临时解决方案

额外检查项

  • Vercel环境变量:确保POSTGRES_URL_NON_POOLING已在Vercel项目的「环境变量」面板中配置,本地.env不会自动同步到Vercel
  • Prisma版本兼容:0.15.0版本较旧,尝试升级到最新稳定版的prisma-client-py,更新requirements.txt后重新部署

内容的提问来源于stack exchange,提问作者Raj Bhut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 01:16:12