Help with queryRawUnsafe
Hey all,
I have a query that previously worked that started to fail. Based off documentation, this is expected since the column names are dynamically generated when using query raw. Can I get help on converting it to a queryRawUnsafe?
Here is a sampe query of what I am trying to convert:
I have tried following this implementation, but ran into unexpected trailing spaces error:
Should I add a validation steps the trims each value or do we have a better approach?
1 Reply
You selected the bot's instantaneous insights. Let me compile your answer right away!
When using
$queryRawUnsafe
, you need to be careful about SQL injection risks. Your second approach is on the right track, but there's a safer way to handle parameterized queries with $queryRawUnsafe
.
The issue with your implementation is that you're directly interpolating values into the SQL string, which can lead to SQL injection vulnerabilities. Instead, you should use parameterized queries:
For PostgreSQL, parameters are represented by $1
, $2
, etc., and you pass the actual values as additional arguments to $queryRawUnsafe
Raw queries with relational databases.
If you're dealing with dynamic table or column names, you can't use parameters for those (SQL doesn't support parameterized identifiers), but you can validate them before using them in your query.
For the "unexpected trailing spaces error," make sure your column and value strings are properly trimmed:
Remember that $queryRawUnsafe
should be used with caution as it increases the risk of SQL injection if not properly handled SQL injection prevention.
GitHub discussions also suggest using Prisma.raw()
for safer dynamic table/column names when possible GitHub Issue: queryRaw doesn't support dynamic tables names.