Hey guys, I'm trying to upload an avatar for authe...
# help
m
Hey guys, I'm trying to upload an avatar for authenticated users, but I keep getting this error:
Copy code
error: "Key is not present in table \"users\"."
message: "insert or update on table \"objects\" violates foreign key constraint \"objects_owner_fkey\""
statusCode: "23503"
I'm not sure of what it means 🤷‍♂️ Maybe my policies are not properly set? Or could it be that the
roles=authenticated
property of the request is not understood by supabase? Any advice about this?
n
Hello @Manuel Coffin! This thread has been automatically created from your message in #843999948717555735 a ``few seconds ago``. Pinging @User so that they see this as well! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ... menu) and select Leave Thread to unsubscribe from future updates. Want to change the title? Use the
/title
command! We have solved your problem? Click the button below to archive it.
g
How are you doing the upload? The error is that you have an owner value that is not null, or a UUID in the auth users table. I assume that value is set to auth.uid() by the storage upload process, but have not looked at the code.
n
manuelcoffin (2022-03-22)
m
I'm calling the function like this:
Copy code
js
const fileExt = values.avatar[0].path.split(".").pop()
            const fileName = `teacher_${profile.id}.${fileExt}`

            let { error: uploadError } = await supabase.storage
              .from("public")
              .upload(`${"avatar"}/${fileName}`, values.avatar[0])
Where
values.avatar[0]
is of type
File
o
Manuel isn't the only one with this issue. 2 more people got this issue, and it looks like they didn't be able to solve it
m
I'm not sure about what you are calling an owner value? Isn't the request to supabase accompanied by a auth token?
g
Yes on jwt token, that is why I was asking how you were calling it...
m
What I find a bit weird is that I used the same code to upload images the other day, and I got it to work, the only difference is that I wasn't using Supabase auth then
I suppose there is an issue with my policies, but even if remove them all, it still doesn't work
o
2 things to try: - Can you logout and signin again with Supabase's api? - Check logs in Settings > logs > API/Database?
m
Ok I'm going to check that, thanks!
g
The error has nothing to do with policies, that I can think of at least. @User are those other situations in discord? I don't find any in quick search of github.
Didn't find any way to solve the issue yet
m
I am able to logout and login again with the supabase api. I can also see the logs, I'm not sure how to interpret the request: POST | 409 | 52.59.247.62 | 6eff9e248a4091e4 | http://[MY_SUPABASE_ID].supabase.co/rest/v1/objects?columns=%22name%22%2C%22owner%22%2C%22bucket_id%22 | node-fetch/1.0 (+https://github.com/bitinn/node-fetch)
o
409 means there is a conflict
it's kinda weird
g
So one of the other cases mentions creating a users table....
m
Humm, I had an user table at the beginning, and it worked, but I removed it because I thought it was redundant with the authentication table of supabase ...
o
it could make sense with the 409 status if for some reasons ``users`` is the same of ``user``, and ``user`` is needed
m
actually the original error I got in the frontend is
Copy code
error: "Key is not present in table \"users\"."
o
like postgres takes the ``user`` name and automatically add the ``s`` at the end because of plural (i already got this issue in another project so)
m
it seem indeed related to the user table
o
yeah, but why
and for sure how to solve it
m
yeah I don't get it either, I thought supabase Authentication could work on its own
o
Let's create an issue on the Github, there is 2 more people getting the error, could be good to report it
m
Ok, I'm going to try to reproduce it on an empty project
g
CONSTRAINT "objects_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id") The fkey seems OK, so not that simple.
m
ah
o
Let's create an issue with a reproduction (if you can) and drop the link here. If someone else has the issue, they gonna be able to have a discuss about it. I'm gonna continue to search what could be the problem, and drop the answer in the issue if found
g
Deleted sorry
m
I keep investigating, now that I added again the Users table, I got a different error:
Copy code
error: "invalid signature"
message: "invalid signature"
statusCode: "400"
Since I'm on a BlitJs project, that has a default session management, I wonder if there could be some interference between the supabase session and the blitzJs session. But I have checked, the authorization token sent in the headers in the same that I get when I call
supabase.auth.session()
g
Just dropping this here... storage does its own decoding of the jwt to get sub(owner id). Which it puts in the owner column before calling postgres (with the original jwt header) to do the insert. RLS would be done on the original header... Nothing wrong with that, but it is different code than auth.uid() with PostgRest doing the decoding.
m
Ok, so I figured the problem I think. I had the auth cookies set multiple times, which caused the invalid signature error. So I removed all my cookies related to supabase and blitz, and now it works! I still need to work on my row level policies now, but it seems to be all good. Thank you guys for your help! No need to open an issue 🙂