Error when signup and save data to profiles table

in my project, I am going to implement a multi-tenant feature. but when I signup as a student, I got this error in browser console. "there is no unique or exclusion constraint matching the ON CONFLICT specification" so the user data is saved correctly in auth.users table but did not save in public profiles table I attached error and the code section screenshot.
No description
No description
16 Replies
garyaustin
garyaustin3w ago
Are you sure that is the code running? The on conflict part usually is associated with doing an Upsert.
Mr.Traveller
Mr.TravellerOP3w ago
I think this code is not working. when I checked the browser console, I saw this error in 239 line.
No description
Mr.Traveller
Mr.TravellerOP3w ago
I checked profiles table, but I think table structure is good, really dont know how to fix this conflict issue
garyaustin
garyaustin3w ago
I don't understand the on conflict error without an upsert. But maybe it is just complaining about you inserting a duplicate key.... wild guess as this is not what the error says. Do you have an insert trigger on the auth.users table to generate a profiles table row? Maybe you are trying to insert and it is already there. Any trigger function on profiles table? Have you ever had an upsert instead of insert to profiles table.
Mr.Traveller
Mr.TravellerOP3w ago
auth.users table, there is not any trigger in profiles table, I saw this trigger
No description
No description
Mr.Traveller
Mr.TravellerOP3w ago
Should I update code from "insert" to "upsert"?
garyaustin
garyaustin3w ago
No. The ON CONFLICT Is part of UPSERT. That is why I don't understand why you are getting it. Show that trigger function code if you can. Also check the Postgres log for what is getting for the error.
Mr.Traveller
Mr.TravellerOP3w ago
okay, let me try
Mr.Traveller
Mr.TravellerOP3w ago
this is trigger function code
No description
garyaustin
garyaustin3w ago
That is likely the source of your error. Is the user_id column UNIQUE in that table?
Mr.Traveller
Mr.TravellerOP3w ago
yes, unique
No description
Mr.Traveller
Mr.TravellerOP3w ago
in profiles table
garyaustin
garyaustin3w ago
The table the trigger is writing to
Mr.Traveller
Mr.TravellerOP3w ago
yeah, this trigger is used in profiles table Did you find issue in my attached images?
garyaustin
garyaustin3w ago
It is inserting to user_statistics With ON CONFLICT on user_id. That means user_id has to be a unique column in user_statistics Or if you plan to have lots of rows of data for a user there, don't use ON CONFLICT
Mr.Traveller
Mr.TravellerOP3w ago
thanks, @garyaustin when I check the user_statistics table, the is_UNIQUE is not set, so I set it and it worked! Thank you so much!

Did you find this page helpful?