select
*
from
domains d
left join
model_responses m
on
d.name = m.domain_name
order by
{{ table1.sortArray.length > 0 ? ${table1.sortArray[0].columnId} ${table1.sortArray[0].direction ? 'DESC' : 'ASC'} : 'm.score DESC' }} NULLS LAST
limit
{{ table1.pagination.pageSize }}
offset
{{ table1.pagination.offset }};
This is not working for some reason. I pasted this into a text box and I get the below query which work in my psql database
select
*
from
domains d
left join
model_responses m
on
d.name = m.domain_name
order by
score DESC NULLS LAST
limit
50
offset
0;
Not sure what is missing but I have been banging my head against the wall for quite a bit on this.
It looks like your query works fine when tested in the text component, but something might be going wrong when executing it within Retool's database query component. Here are a few things to check:
1. Check if table1.sortArray Exists and is Populated
If table1.sortArray is empty, the sorting logic defaults to 'm.score DESC'.
2. Verify the Sorting Logic
The query dynamically builds the ORDER BY clause, but if table1.sortArray[0].columnId is undefined, the query might break.
Test this manually by replacing table1.sortArray values with static values in the text component.
Welcome to the community, @Kiran_Nair! Thanks for reaching out.
I'm guessing that the content of the text component is exactly the same as the query? If that's the case, it seems likely that the process of converting it to a prepared statement is responsible for the behavior that you're seeing.
You can verify this by disabling prepared statements via the resource configuration.
If that does fix it, we might have to figure out an alternative workaround, as we don't really recommend leaving prepared statements disabled. You can read more about it here!