# Supabase results not properly type cast in weweb

**URL:** <https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045>\
**Category:** Ask us anything\
**Created:** [June 10, 2024, 10:00pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045 "2024-06-10T22:00:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![oblic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@oblic](https://community.weweb.io/u/oblic)\
**Post date:** [June 10, 2024, 10:00pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/1 "2024-06-10T22:00:25Z")

</div>

I have a fairly complex query in a supabase DB function.

The function works fine when I test it within supabase query editor.

When I invoke the function from weweb, I get null results for a specific field.  
Now, This field can either be null or a text value (I forced a type cast on it in supabase).  
I think weweb is not handling it properly because the value is sometimes null.

Anyone ever got a similar problem ?  
As mentioned, everything is fine prior to the webweb handling of the results.

SQL Results  
 ![Screenshot 2024-06-10 at 5.57.25 PM](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/b/b156dedaae8b29e117d07c0e7a8808d161a3e20b.png)

Weweb results (response = null, should be “Non”)  
 ![Screenshot 2024-06-10 at 5.58.46 PM](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/8/8f7e612f63dd9c327dba0787b2fb94ef010f2e03.png)

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [June 10, 2024, 10:13pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/2 "2024-06-10T22:13:55Z")

</div>

I think sharing the query might help. There might be some issues with how the PostgREST (which is the library that creates the REST layer on top of Supabase) handles your function’s output.

---

<div class="post-metadata">

**Author:** ![oblic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@oblic](https://community.weweb.io/u/oblic)\
**Post date:** [June 10, 2024, 10:16pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/3 "2024-06-10T22:16:55Z")

</div>

* * *

It’s a fairly big query. response is the culprit, everything else works great.

CREATE OR REPLACE FUNCTION get\_exp\_questionnaire\_from\_autoeval(i\_entreprise\_id UUID)  
RETURNS TABLE(  
thematique\_name TEXT,  
enjeu\_name TEXT,  
enjeu\_description TEXT,  
enjeu\_image TEXT,  
criteria\_name TEXT,  
criteria\_description TEXT,  
criterion\_type TEXT,  
criteria\_id TEXT,  
criterion\_id TEXT,  
question\_id UUID,  
question TEXT,  
response TEXT  
) AS $$  
BEGIN  
RETURN QUERY  
WITH full\_questionnaire AS (  
SELECT  
t.name AS thematique\_name,  
e.name AS enjeu\_name,  
e.description AS enjeu\_description,  
e.image AS enjeu\_image,  
c.name AS criteria\_name,  
c.description AS criteria\_description,  
cr.type::TEXT AS criterion\_type,  
c.id::TEXT AS criteria\_id,  
cr.id::TEXT AS criterion\_id,  
q.id AS question\_id,  
cr.question AS question  
FROM  
“Entreprise” ent  
JOIN “Sector\_Questionnaires” s ON ent.sector\_id = s.sector\_id  
JOIN “Questionnaires” qn ON s.questionnaire\_id = qn.id  
JOIN “Questionnaire\_context” qct ON qn.id = qct.questionnaire\_id  
JOIN “Question” q ON qn.id = q.questionnaire\_id  
JOIN “Criteria” c ON q.criteria\_id = c.id  
JOIN “Enjeu” e ON c.enjeu\_id = e.id  
JOIN “Thematique” t ON e.thematique\_id = t.id  
JOIN “Criterion\_context” cct ON qct.id = cct.questionnaire\_context  
JOIN “Criterion” cr ON cct.criterion\_id = cr.id  
WHERE  
ent.id = i\_entreprise\_id  
ORDER BY  
t.name, e.name, c.name, cr.order\_type  
),  
auto\_eval\_responses AS (  
SELECT  
c.id::TEXT AS criteria\_id,  
cr.id::TEXT AS criterion\_id,  
**r.display\_text::TEXT AS response**  
FROM  
“Evaluations” eval  
JOIN “Evaluation\_Response” resp ON resp.eval\_id = eval.id  
JOIN “Criteria” c ON resp.criteria\_id = c.id  
JOIN “Criterion” cr ON resp.criterion\_id = cr.id  
JOIN “Response” r ON r.id = resp.response\_id  
WHERE  
eval.entreprise\_id = i\_entreprise\_id  
)  
SELECT  
fq.thematique\_name,  
fq.enjeu\_name,  
fq.enjeu\_description,  
fq.enjeu\_image,  
fq.criteria\_name,  
fq.criteria\_description,  
fq.criterion\_type,  
fq.criteria\_id,  
fq.criterion\_id,  
fq.question\_id,  
fq.question,  
**aer.response::TEXT**  
FROM  
full\_questionnaire fq  
LEFT JOIN  
auto\_eval\_responses aer  
ON  
fq.criteria\_id = aer.criteria\_id AND fq.criterion\_id = aer.criterion\_id  
ORDER BY  
fq.thematique\_name, fq.enjeu\_name, fq.criteria\_name, fq.criterion\_type;  
END;  
$$ LANGUAGE plpgsql;

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [June 10, 2024, 10:21pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/4 "2024-06-10T22:21:33Z")

</div>

The query indeed seems fine. What I probably would do is try to curl it, or try to somehow call the endpoint via REST to assess that the issue actually is WeWeb. You can call the functions via the Supabase’s REST API, you can find how to do this in your API Docs.

You might also try to COALESCE the missing field and see if the issue is coming from the DB - you’d get your coalesced fallback, or if it just straight gives you null. I’d probably do this first.

---

<div class="post-metadata">

**Author:** ![oblic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@oblic](https://community.weweb.io/u/oblic)\
**Post date:** [June 10, 2024, 10:33pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/5 "2024-06-10T22:33:57Z")

</div>

I did try the coalesce. Changed null to “”, but result was basically the same.

I did this :

- Created a DB Function that creates a dynamic view based on specific client\_id
- Create a dynamic collection in weweb that connects to that dynamic view

Result is now fine and my fields all have expected values.  
Seems to point to a weweb issue…

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [June 10, 2024, 10:37pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/6 "2024-06-10T22:37:37Z")

</div>

I’d still probably do some more due diligence and give this a shot, also because WeWeb takes a little more to answer tickets, than it takes you to test this out:

> [@Broberto](#):
>
> What I probably would do is try to curl it, or try to somehow call the endpoint via REST to assess that the issue actually is WeWeb.

You can also use postman for this, and you can get your auth token for the Authorization header from WeWeb, so this should be super quick.

After that, if you conclude that it’s indeed a bug, you can open a ticket at [https://support.weweb.io/](https://support.weweb.io/)  
I personally never had this kind of issue with WeWeb, but it’s definitely not impossible.

> [@oblic](#):
>
> Create a dynamic collection in weweb that connects to that dynamic view

Edit: One thing that actually comes to my mind is that views are SECURITY DEFINER by default. Maybe RLS might be playing a role? Sounds impossible, but might as well be so. You’d probably find out by curling the function with your currrent user’s token.

Edit 2: Based on your query the response seems to be in a standalone table, so maybe it is the RLS after all? By the way - you don’t even need to curl it, you can just simply [impersonate that user you’re invoking the function as in WeWeb from the Supabase SQL Dashboard.](https://supabase.com/blog/studio-introducing-assistant#user-impersonation) This should rule out the RLS being an issue.

 ![Screenshot 2024-06-11 at 00.46.01](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/5/58cb4c2ec751c364b3a95fc38038b299e1836e49.jpeg)

---

<div class="post-metadata">

**Author:** ![oblic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@oblic](https://community.weweb.io/u/oblic)\
**Post date:** [June 11, 2024, 1:18pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/7 "2024-06-11T13:18:34Z")

</div>

That is great info. Thanks for the advice.

I tried impersonating different roles and always get good results.  
Most of my RLS aren’t defined yet as I’m still in dev mode. I usually come back to RLS once a feature is done.

I will create a ticket because this is a show stopper for me. To my knowledge, collections can’t be created dynamically to fit my dynamic view so I can’t really use this except for testing.

---

<div class="post-metadata">

**Author:** ![oblic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@oblic](https://community.weweb.io/u/oblic)\
**Post date:** [June 11, 2024, 1:34pm UTC](https://community.weweb.io/t/supabase-results-not-properly-type-cast-in-weweb/9045/8 "2024-06-11T13:34:49Z")

</div>

I FOUND THE BUG ! It’s me … lol 🫥

There was a confusion in the sent parameter to the DB function causing the wrong id to be sent.  
I was getting null results because the results for this id are null.  
It was hardcoded in my SQL test so I never noticed it.

Sorry for taking some of your time, thank you for your inputs. I still got to learn some interesting technical details along the way.

Thanks again.
