Home > database >  Using Prisma, how can I Insert using $executeRaw function and return the values inserted?
Using Prisma, how can I Insert using $executeRaw function and return the values inserted?

Time:01-21

I am trying the following Postgresql query with Prisma's $executeRaw function. But it is not returning the values inserted. Instead only returning the number of records inserted.

await prismaClient.$executeRaw(`
INSERT INTO table1 (name, place, animal, thing)
SELECT * FROM table2
WHERE place = 'California'
RETURNING *;
`);

It is returning

1

While I want the records inserted to be returned. How can that be done?

CodePudding user response:

You are making insert and select from diferent tables. You are inserting in table1 and selecting from table2. It could be the problem.

Try this.

await prismaClient.$executeRaw(`
INSERT INTO table1 (name, place, animal, thing)
SELECT * FROM table1
WHERE place = 'California';
`);

CodePudding user response:

Thanks to jian I got the answer

await prismaClient.$queryRaw(`
  INSERT INTO table1 (name, place, animal, thing)
  SELECT * FROM table2
  WHERE place = 'California'
  RETURNING *;
`);
  • Related