Demo, all content is generated
Question

Signup broke: 'Database error saving new user'

Solved · 1102 views · asked by bakery_bo · edited

My bakery preorder app. Signup worked for weeks. I asked Lovable to add a phone number to profiles, and since then every signup shows "Database error saving new user". Existing users can still log in. I have no idea what a trigger is, but I think there is one.

What I’ve tried

Reverted the Lovable change in the UI, error stays. Tried signing up with different emails.

Comment
Check Logs > Postgres in the dashboard first, the real error is there. mira_dev · edited

3 answers

Marked as helpful by the asker
postgres_pete · edited

Auth creates the user, then your trigger (on_auth_user_created, usually calling handle_new_user()) inserts into profiles. If that insert fails, the whole signup fails with this generic message. Reverting the UI didn't revert the database.

Find the real error in the dashboard: Logs > Postgres, filter on "error" around the time of a signup. Common causes after "add a phone number":

  • phone was added as not null, and the trigger doesn't fill it. Make it nullable.
  • The function references a column that got renamed.

Look at the function:

select pg_get_functiondef('public.handle_new_user'::regproc);

and make it match the table, for example:

create or replace function public.handle_new_user()
returns trigger language plpgsql security definer set search_path = ''
as $$
begin
  insert into public.profiles (id, email, phone)
  values (new.id, new.email, new.raw_user_meta_data ->> 'phone');
  return new;
end;
$$;
Comment
The log said: null value in column "phone" violates not-null constraint. Made it nullable. Signups work!! Thank you, I had 3 customers emailing me. bakery_bo · edited
mira_dev · edited

Tip for the future: have the trigger insert only id and let the app fill the rest after signup. The less a trigger on auth.users does, the fewer ways it can break signups.

Comment
I'll ask Lovable to do it that way next time, thanks bakery_bo · edited
wes_codes · edited

For next time: Lovable's revert only rolls back the code. Anything it ran against the database stays. Look in the supabase/migrations folder of the GitHub repo it syncs to, that shows exactly what SQL ran.

Comment