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

使用Prisma存入PostgreSQL的数据在psql中无法查询的问题

问题描述

我使用Prisma创建了如下数据库schema:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

model User {
  id            String       @default(cuid()) @id
  name          String?
  email         String?   @unique
  createdAt     DateTime  @default(now()) @map(name: "created_at")
  updatedAt     DateTime  @updatedAt @map(name: "updated_at")
  is_confirmed  Boolean   @default(false)
  confirm_key   String
  @@map(name: "users")
}  

我的.env文件配置如下:

# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://POSTGRES_USERNAME:PASSWORD@localhost:5432/DB_NAME?schema=users"  

我执行了npx prisma db push命令,终端显示数据库已与schema同步,且生成了Prisma Client。随后我通过npx prisma studio添加了2条数据,但在docker容器中通过psql登录数据库后,执行SELECT * FROM User;仅返回用户名,无法找到新增的数据。请问我哪里操作有误?

问题原因及解决方法

1. 表名匹配错误

你的Prisma模型User通过@@map(name: "users")指定了数据库中的实际表名为users,并非User。PostgreSQL默认对表名大小写不敏感,直接查询User会匹配到非目标表,导致找不到数据。

解决:执行正确的查询语句:

SELECT * FROM users;

2. Schema未切换

你的DATABASE_URL中指定了schema=users,意味着Prisma操作的是users这个独立schema下的表,但psql登录后默认使用PostgreSQL的public默认schema,自然看不到目标数据。

解决方法二选一:

  • 方法一:查询时指定schema前缀:
SELECT * FROM users.users;
  • 方法二:先切换到目标schema再查询:
SET search_path TO users;
SELECT * FROM users;

验证步骤

  1. 登录psql后,先确认当前使用的schema:
SHOW search_path;
  1. 查看users schema下的表列表:
\dt users.*

如果能看到users表,再执行查询就能找到你通过Prisma Studio添加的数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 14:20:26