Database error saving new user when using trigger
Hey, I'm using supabase and prisma and i'm having trouble with triggers. I have a handle_new_user function that's triggered whenever a user signs up and it's erroring and preventing you from signing up. posted pics of my function and user model. any help would be appreciated, ty!


33 Replies
What is your error? It should be in the Postgres logs.
theres an error in the auth logs but nothing in postgres
OK. You implied it was the trigger so assumed you just added that and it broke. Does it work without the trigger?
yeah it does work without the trigger. i tried it without and then after adding it it stopped working
You sure there is nothing in the Postgres log.
no errors at least
just this

And that time frame covers your test?
yep
oh i just tried again and i got an error

The auth error is just saying the insert failed. We presume based on your test because of the trigger function. This is very common to happen. But there are all kinds of reasons it could fail. I'm not familiar with a case without an error though....
OK that is implying you have a uuid column and are inserting text, or a text column and you are inserting a uuid.
hm thats weird because im just trying to set id and email. my user schema has uuid as the type for id so idk why it would be erroring
Yeah, I agree. Based on what you show.
Can you show a picture from the table UI of the actual public users table?
Can you show a picture from the table UI of the actual public users table?
yeah

Also you can add
raise log 'new = %',new; after begin in your function and look in the log to make sure the function is being called.oh sure
Also are you sure you have just the one trigger and function?
yep its being called

i have just one for signup
i also have delete and update
update also gets called on new user
oh
this is my update function

ohh text should be id
After doing the insert, it will comeback and update the raw_app_meta_data column.
uuid*
update gets called alot. You really should put a WHEN clause in the trigger for things changing you care about, or check in the function.
For instance on every sign in there is an update.
ah so add a when so it only gets called when specific fields are changed?
There is an option for that in the trigger itself. (at least in SQL there is).
how would i go about doing that? sorry im pretty new to this
https://www.postgresql.org/docs/current/sql-createtrigger.html See the WHEN examples here.
PostgreSQL Documentation
CREATE TRIGGER
CREATE TRIGGER CREATE TRIGGER — define a new trigger Synopsis CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER name …
great tysm for your help!