Prisma
prisma.io › home › raw queries › raw queries › raw queries › raw queries
Raw queries | Prisma Documentation
Prisma Client specifically uses SQL Template Tag, which exposes a number of helpers. For example, the following query uses join() to pass in a list of IDs:
GitHub
github.com › prisma › prisma › issues › 6276
Error when using `join` raw query helper with SQL Server · Issue #6276 · prisma/prisma
March 26, 2021 - Code: `245`. Message: `Conversion ... ... Run a raw query with the following bit in the where clause: IN (${Prisma.join(ids)}) - where ids is an array of numbers....
Author prisma
How to Join and Union in rawQuery
I tried to use join in the SQL query but I got this error · prisma.$queryRaw()` invocation: Raw query failed. More on github.com
postgresql - Prisma $queryRaw with variable length parameter list - Stack Overflow
But when you use uuid type, Prisma's join function formats the parameters adding double apostrophe. So you can take error like 'type mismatch' in PostreSQL. Alternatively, you can use native Array.join function to build a sql string and send it to Prisma raw query function using Prisma.raw function. More on stackoverflow.com
SQL joins without relations/foreign keys/raw query
Problem I sometimes find myself wanting to join 2 tables that are related by a column, but don't have referential integrity. For example, let's say I have 2 tables, Transaction and SmartCon... More on github.com
using `queryRaw` with `sql` to do javascript functions?
Thank you for your understanding and ongoing support of the Prisma community! Beta Was this translation helpful? Give feedback. ... Sign up for free to join this conversation on GitHub. Already have an account? More on github.com
GitHub
github.com › prisma › prisma › discussions › 24838
Queryraw condition joining · prisma/prisma · Discussion #24838
July 23, 2024 - The conditions array is joined with AND using Prisma.join with a string separator ' AND '. The final query is constructed using Prisma.sql, ensuring that the SQL string and the parameters are correctly aligned.
Author prisma
GitHub
github.com › prisma › prisma › discussions › 20563
How to Join and Union in rawQuery · prisma/prisma · Discussion #20563
August 8, 2023 - prisma.$queryRaw` SELECT id, amount, title, updatedAt, type, name AS category FROM ( SELECT e.id, e.amount, e.title, e.updatedAt, e.type, ec.name FROM "Expense" e INNER JOIN "CategoriesOnExpense" ce ON e.id = ce."expenseId" INNER JOIN "ExpenseCategory" ec ON ce."categoryId" = ec.id WHERE e."ownerId" = ${userId} AND e."year" = ${year} AND e."month" = ${month} UNION SELECT i.id, i.amount, i.title, i.updatedAt, i.type, ic.name FROM "Income" i INNER JOIN "CategoriesOnIncome" ci ON i.id = ci."incomeId" INNER JOIN "IncomeCategory" ic ON ci."categoryId" = ic.id WHERE i."ownerId" = ${userId} AND i."year" = ${year} AND i."month" = ${month} ) AS result ORDER BY result."updatedAt" DESC; `;
Author prisma
GitHub
github.com › prisma › prisma › discussions › 17158
Joined Relations with $queryRaw · prisma/prisma · Discussion #17158
The $queryRaw method will return the data as it is retrieved from the database, which means that if you perform a join in your raw SQL query, the result will be a flat structure with columns from both tables.
Author prisma
MCP Servers
mcpservers.org › home › agent skills library › prisma-client-api-raw-queries
prisma-client-api-raw-queries | Agent Skills Library
import { Prisma } from '../generated/client' const column = 'email' const users = await prisma.$queryRaw` SELECT ${Prisma.raw(column)} FROM "User" ` ... import { Prisma } from '../generated/client' const email = '[email protected]' const query = Prisma.sql`SELECT * FROM "User" WHERE email = ${email}` const users = await prisma.$queryRaw(query) import { Prisma } from '../generated/client' const conditions = [ Prisma.sql`role = ${'ADMIN'}`, Prisma.sql`verified = ${true}` ] const users = await prisma.$queryRaw` SELECT * FROM "User" WHERE ${Prisma.join(conditions, ' AND ')} `
Author prisma
Top answer 1 of 2
52
RTFM: https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#tagged-template-helpers
import { Prisma } from "@prisma/client";
const ids = [1, 3, 5, 10, 20];
const result = await prisma.$queryRaw`SELECT * FROM User WHERE id IN (${Prisma.join(
ids
)})`;
2 of 2
7
As @MichaelDausmann has mentioned above, there is a Prisma function that joins array items and formats the SQL by the types of parameters. But when you use uuid type, Prisma's join function formats the parameters adding double apostrophe. So you can take error like 'type mismatch' in PostreSQL. Alternatively, you can use native Array.join function to build a sql string and send it to Prisma raw query function using Prisma.raw function.
const sql = `select * from table where id in (${idArray.map(v => `'${v}'::uuid`).join(",")})`
result = await prisma.$queryRaw(Prisma.raw(sql))
GitHub
github.com › prisma › prisma › issues › 13517
SQL joins without relations/foreign keys/raw query · Issue #13517 · prisma/prisma
May 27, 2022 - Problem I sometimes find myself wanting to join 2 tables that are related by a column, but don't have referential integrity. For example, let's say I have 2 tables, Transaction and SmartCon...
Author prisma
New Releases
newreleases.io › project › github › prisma › prisma › release › 2.0.0-beta.6
prisma/prisma 2.0.0-beta.6 on GitHub
May 26, 2020 - In order to construct raw SQL queries programmatically, Prisma Client now exposes a few helper functions: import { sql, empty, join, raw, PrismaClient } from '@prisma/client' const prisma = new PrismaClient() const rawQueryTemplateFromSqlTemplate ...
DEV Community
dev.to › this-is-learning › its-prisma-time-execute-your-own-queries-4olp
It's Prisma Time - Execute your own queries - DEV Community
January 28, 2022 - This method uses the Tagged Template and it prevents attack by SQL-injection too. The result of this method is always an array and the type of this array depends on the result of your query. To explain better this concept let me give an example. const result: Post[] = await prisma.$queryRaw<Post[]>` SELECT p.id, p.title, p.content, p.published, p.createAt, p.updatedAt FROM posts p WHERE p.published = ${true} ORDER BY p.createAt DESC`; result.forEach(post => { const { id, title, content, createAt, published, updatedAt } = post; console.log({ id, title, content, createAt, published, updatedAt, }); });
Prisma
prisma.io › dataguide › types › relational › what-are-joins-in-sql
What are JOINs in SQL? | Prisma's Data Guide
Here is an example SQL schema demonstrating a one-to-many relation between users and posts tables: ... CONSTRAINT fk_posts_author_id FOREIGN KEY (author_id) REFERENCES users (id) ON DELETE RESTRICT ON UPDATE CASCADE ... This means that one user can have multiple posts, but each post is linked to only one user. When dealing with one-to-many relations, traditional JOINs can lead to duplicated data. Consider the following query:
Stack Overflow
stackoverflow.com › questions › 74274673 › raw-query-in-prisma-query-console
Raw query in Prisma Query Console - Stack Overflow
The workaround is to use $queryRawUnsafe. The syntax is · await prisma.$queryRawUnsafe('SELECT * FROM "public"."User"')