Database error saving new user
当你尝试从仪表板邀请新用户,或者在 Supabase 仪表板使用表格编辑器向表中插入用户时,通常会遇到这个错误。你也可能在日志中看到这个错误,通常是由于注册失败引起的。
🌐 You generally get this error when trying to invite a new user from the dashboard or when trying to insert a user into a table using the table editor in the Supabase dashboard. You may also see this error in logs in connection to failed signups.
这个错误通常与数据库事务的副作用有关。
🌐 This error is normally associated with a side effect of a database transaction.
这个错误的常见原因:
- 你在
auth.users表上设置的触发器/触发器函数有错误 - 你在
auth.users表上添加了一个约束,但没有被满足 - 你在用 Prisma,它把
auth.users表的所有权限都搞乱了
调试这个错误: #
🌐 Debugging this error:
步骤 1:检查认证日志
在你的项目仪表板中,从 Auth 日志浏览器 开始。日志会显示具体的错误信息,并直接告诉你出了什么问题。
🌐 Start in the Auth logs explorer in your project dashboard. The logs surface the specific error message and give you the most direct signal about what went wrong.
步骤 2:查看 Postgres 日志
如果认证日志显示是数据库层面的问题,请打开 Postgres 日志浏览器 查找对应的错误。
🌐 If the Auth logs point to a database-level issue, open the Postgres logs explorer to look for corresponding errors.
常见错误 #
🌐 Common errors
认证模式列中的空值 #
🌐 NULL value in auth schema column
示例认证日志错误:
1500: Database error querying schema2error finding user: sql: Scan error on column index 8, name "confirmation_token": converting NULL to string is unsupported原因:
auth 模式由 Supabase 管理,并且期望特定的列格式。这个错误通常发生在用户通过 SQL 直接插入,或者使用 AI 工具直接执行 SQL INSERT 时,而不是通过 Auth API。直接插入可能会让某些必填列变成 NULL,而 Auth 服务其实期望它们是空字符串。
🌐 The auth schema is managed by Supabase and expects specific column formats. This error typically happens when users are inserted directly via SQL or using an AI tool that uses a direct SQL INSERT, rather than through the Auth API. A direct insert can leave required columns as NULL when the Auth service expects an empty string.
修复:
在 SQL 编辑器 中运行以下内容,并将 confirmation_token 替换为错误信息中出现的列:
🌐 Run the following in the SQL editor, replacing confirmation_token with whichever column appears in your error message:
1update auth.users2set confirmation_token = ''3where confirmation_token is null;这会把那一列的所有 NULL 值都设置为空字符串,这正是认证服务所期望的。
🌐 This sets all NULL values in that column to an empty string, which is what the Auth service expects.
关系不存在 #
🌐 Relation does not exist
示例认证日志错误:
1failed to close prepared statement: ERROR: current transaction is aborted, commands ignored until end of transaction block (SQLSTATE 25P02): ERROR: relation "profiles" does not exist (SQLSTATE 42P01)示例 Postgres 日志错误:
1event_message: "relation "profiles" does not exist"2context: "PL/pgSQL function public.handle_new_user() line 3 at SQL statement"原因:
auth.users 上的触发器正在调用一个函数(在这个例子中是 handle_new_user),这个函数试图向一个不存在的表插入数据。这种情况很常见,比如触发器函数里引用了 profiles 表,但这个表从来没有创建过,或者被意外删除了。
🌐 A trigger on auth.users is calling a function (in this case handle_new_user) that tries to insert into a table that does not exist. This is common when a profiles table is referenced in a trigger function but was never created, or was accidentally dropped.
修复:
- 检查一下缺失的表是否应该存在于你的
public架构中。如果应该存在,创建这个表,这样触发器就能成功运行,错误也会解决。 - 如果不需要这个表,请查看函数定义并更新或删除引用。你可以在仪表板的数据库函数页面查看和编辑你的数据库函数。