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!
No description
No description
33 Replies
garyaustin
garyaustin2y ago
What is your error? It should be in the Postgres logs.
ethan
ethanOP2y ago
theres an error in the auth logs but nothing in postgres
{"auth_event":{"action":"user_signedup","actor_id":"00000000-0000-0000-0000-000000000000","actor_username":"service_role","actor_via_sso":false,"log_type":"team","traits":{"user_email":"intuitiveen@gmail.com","user_id":"5a7cf80f-475a-4b33-868c-99367e98234f","user_phone":""}},"component":"api","error":"ERROR: operator does not exist: uuid = text (SQLSTATE 42883)","level":"error","method":"POST","msg":"500: Database error creating new user","path":"/admin/users","referer":"http://localhost:3000","remote_addr":"68.196.164.104","time":"2023-12-30T19:56:02Z","timestamp":"2023-12-30T19:56:02Z"}
{"auth_event":{"action":"user_signedup","actor_id":"00000000-0000-0000-0000-000000000000","actor_username":"service_role","actor_via_sso":false,"log_type":"team","traits":{"user_email":"intuitiveen@gmail.com","user_id":"5a7cf80f-475a-4b33-868c-99367e98234f","user_phone":""}},"component":"api","error":"ERROR: operator does not exist: uuid = text (SQLSTATE 42883)","level":"error","method":"POST","msg":"500: Database error creating new user","path":"/admin/users","referer":"http://localhost:3000","remote_addr":"68.196.164.104","time":"2023-12-30T19:56:02Z","timestamp":"2023-12-30T19:56:02Z"}
garyaustin
garyaustin2y ago
OK. You implied it was the trigger so assumed you just added that and it broke. Does it work without the trigger?
ethan
ethanOP2y ago
yeah it does work without the trigger. i tried it without and then after adding it it stopped working
garyaustin
garyaustin2y ago
You sure there is nothing in the Postgres log.
ethan
ethanOP2y ago
no errors at least
ethan
ethanOP2y ago
just this
No description
garyaustin
garyaustin2y ago
And that time frame covers your test?
ethan
ethanOP2y ago
yep oh i just tried again and i got an error
ethan
ethanOP2y ago
No description
garyaustin
garyaustin2y ago
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.
ethan
ethanOP2y ago
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
garyaustin
garyaustin2y ago
Yeah, I agree. Based on what you show.
Can you show a picture from the table UI of the actual public users table?
ethan
ethanOP2y ago
yeah
ethan
ethanOP2y ago
No description
garyaustin
garyaustin2y ago
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.
ethan
ethanOP2y ago
oh sure
garyaustin
garyaustin2y ago
Also are you sure you have just the one trigger and function?
ethan
ethanOP2y ago
yep its being called
ethan
ethanOP2y ago
No description
ethan
ethanOP2y ago
i have just one for signup i also have delete and update
garyaustin
garyaustin2y ago
update also gets called on new user
ethan
ethanOP2y ago
oh
ethan
ethanOP2y ago
this is my update function
No description
ethan
ethanOP2y ago
ohh text should be id
garyaustin
garyaustin2y ago
After doing the insert, it will comeback and update the raw_app_meta_data column.
ethan
ethanOP2y ago
uuid*
garyaustin
garyaustin2y ago
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.
ethan
ethanOP2y ago
ah so add a when so it only gets called when specific fields are changed?
garyaustin
garyaustin2y ago
There is an option for that in the trigger itself. (at least in SQL there is).
ethan
ethanOP2y ago
how would i go about doing that? sorry im pretty new to this
garyaustin
garyaustin2y ago
PostgreSQL Documentation
CREATE TRIGGER
CREATE TRIGGER CREATE TRIGGER — define a new trigger Synopsis CREATE [ OR REPLACE ] [ CONSTRAINT ] TRIGGER name …
ethan
ethanOP2y ago
great tysm for your help!

Did you find this page helpful?