Skip to content

Resolving 42P01: relation does not exist error

42P01 是一个 Postgres 级别错误,也可以在 PostgREST 错误文档 中找到

1
42P01: relation "<some table name>" does not exist

可能有几个原因 #

🌐 There are a few possible causes


原因 1:搜索路径断了 #

🌐 Cause 1: Search path broken

当直接访问不在 public 模式中的表时,重要的是在查询中明确引用外部模式。下面是来自 JS 客户端: 的一个示例

🌐 When directly accessing a table that is not in the public schema, it's important to reference the external schema explicitly in your query. Below is an example from the JS client:

1
const { data, error } = await supabase.schema('myschema').from('mytable').select()

如果在直接调用表后,你遇到 42501 权限被拒绝的错误,那么你还必须 将自定义模式暴露给 API。 对于 Supabase 管理的模式,比如 vaultauth,出于安全原因,这些不能通过 DB REST API 直接访问。如有必要,可以严格通过 安全定义函数访问。

🌐 If after calling the table directly, you get a 42501 permission denied error, then you must also expose the custom schema to the API.. For Supabase managed schemas, such as vault and auth, these cannot be directly accessed through the DB REST API for security reasons. If necessary, they can be strictly accessed through security definer functions.


原因2:忽略大小写和其他拼写错误 #

🌐 Cause 2: Ignoring capitalization and other typos

这个表可以定义为:CREATE TABLE “Hello”。双引号让它区分大小写,所以调用表时必须使用正确的名称。为了方便,也可以把表名改成小写,可以在表格编辑器里改,或者直接用原生 SQL 改:

🌐 The table could be defined as: CREATE TABLE “Hello”`. The double quotes make it case-sensitive, so it becomes essential to call the table with the appropriate title. It is possible to change the table name to be lowercase for convenience, either in the Table Editor, or with raw SQL:

1
alter table "Table_name"
2
rename to table_name;

原因3:表或函数不存在 #

🌐 Cause 3: Table or function does not exist

有人可能从未制作过这张桌子,也可能没有故意或意外地把它弄掉。可以用以下查询来检查:

🌐 One may have never made the table or dropped it deliberately or accidentally. This can be checked with the following query:

1
-- For tables
2
SELECT * FROM information_schema.tables
3
WHERE table_name ILIKE 'example_table'; --<------ Add relevant table name
1
-- For functions
2
select
3
p.proname as function_name,
4
n.nspname as schema_name,
5
pg_get_functiondef(p.oid) as function_definition
6
from
7
pg_proc as p
8
join pg_namespace as n on p.pronamespace = n.oid
9
where n.nspname in ('public', 'your custom schema') -- <------ Add other relevant schemas
10
order by n.nspname, p.proname;