Triggers not working properly with Prisma: [ERROR] `Query '' is required to return data, but found no record(s).`
See original GitHub issueBug description
Hello
I’m having trouble when trying to insert a new record to my shipment
table, I have a trigger that is in charge of creating a custom unique ID. (you can see the sql script in the How to reproduce
section.
NOTE: Whenever I drop the trigger from the table, I can create a record without an error.
Couple of notes
- The trigger works when I use pgAdmin to execute raw sql scripts.
- Whenever I drop the trigger from the table, I can create a record without an error.
This is the error that I get
/Users/gmwill934/Documents/gmwill934/prisma-trigger-error/node_modules/@prisma/client/runtime/index.js:28842
throw new PrismaClientUnknownRequestError(message, this.client._clientVersion);
^
PrismaClientUnknownRequestError:
Invalid `prisma.shipment.create()` invocation in
/Users/gmwill934/Documents/gmwill934/prisma-trigger-error/prisma.ts:15:34
12 await prisma.$executeRawUnsafe(setClient("NPS"));
13 await prisma.$executeRawUnsafe(setWarehouse("01"));
14
→ 15 return await prisma.shipment.create(
Query createOneshipment is required to return data, but found no record(s).
at RequestHandler.handleRequestError (/Users/gmwill934/Documents/gmwill934/prisma-trigger-error/node_modules/@prisma/client/runtime/index.js:28842:13)
at RequestHandler.request (/Users/gmwill934/Documents/gmwill934/prisma-trigger-error/node_modules/@prisma/client/runtime/index.js:28820:12)
at async PrismaClient._request (/Users/gmwill934/Documents/gmwill934/prisma-trigger-error/node_modules/@prisma/client/runtime/index.js:29753:16) {
clientVersion: '4.2.1'
}
How to reproduce
- On pgAdmin (postgres), run the following
sql script
to create the table.
create table shipment (
client varchar(16) not null default(current_setting('app.client')::varchar(16)),
warehouse varchar(16) not null default(current_setting('app.warehouse')::varchar(16)),
id varchar(64) default(''),
notes text null,
is_validated boolean not null default(false),
constraint "PK_Shipment__id" primary key (id)
);
- Run the following
sql script
to create the trigger function
create or replace function shipment_custom_id_generator() returns trigger as $$
begin
new.id = (select
current_setting('app.client') || current_setting('app.warehouse') || 'LPN' || LPAD(cast(count(*) + 1 as varchar(64)),8,'0')::varchar(16)
from
shipment
where
client = current_setting('app.client')
and warehouse = current_setting('app.warehouse')
);
return new;
end
$$ language plpgsql
- Create the trigger
create trigger shipment_custom_generator_id_trigger
before insert on shipment
for each row
execute procedure shipment_custom_id_generator();
- Clone the repo https://github.com/gmwill934/prisma-trigger-error
- Install dependencies
- Create a .env file with a
DATABASE_URL
attribute. Make sure it matches your local pg environment. (DATABASE_URL="postgresql://user:password@localhost:port/database-name?schema=schema-name"
) - Run
npm run prisma:pull
to introspect the database - Generate prisma client running
npm run prisma:generate
- Run the app with
npm run dev
Expected behavior
Insert a new record on the shipment
table without a 500
error code.
Prisma information
This is the my prisma schema
model shipment {
client String @default(dbgenerated("(current_setting('app.client'::text))::character varying(16)")) @db.VarChar(16)
warehouse String @default(dbgenerated("(current_setting('app.warehouse'::text))::character varying(16)")) @db.VarChar(16)
id String @id(map: "PK_Shipment__id") @default("") @db.VarChar(64)
notes String?
is_validated Boolean @default(false)
}
Environment & setup
- OS: Mac OS M1
- Database: Postgres 14
- Node.js version: v16.15.1
Prisma Version
4.2.1
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Upsert error on MySQL: Query ... is required to return data, but ...
The emailAddress column is required to be unique. The error occurs with code that looks rather like this: const user = await prisma....
Read more >Error message reference - Prisma
Prisma Client throws a PrismaClientUnknownRequestError exception if the query engine returns an error related to a request that does not have an error...
Read more >Prisma failed queries don't return proper errors - Stack Overflow
For example PrismaClientKnownRequestError has code property, but PrismaClientValidationError does not and so on. Example: try { await client.
Read more >How To Build a REST API with Prisma and PostgreSQL
Prisma is an open-source ORM for Node.js and TypeScript. It consists of three main tools: Prisma Client: An auto-generated and type-safe query ......
Read more >3 common foreign key mistakes (and how to avoid them)
Foreign key constraints are important to any SQL database, but they can also cause problems if they're not implemented correctly.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Tried workarounds but no luck, data is not unique. Hoping the team could address this at some point.
Thank you very much @janpio
For sure @janpio
https://github.com/gmwill934/prisma-simple
andcd
to new created foldernpm i
npx prisma init
DATABASE_URL
property on your .env file to your environmentprisma/schema.prisma
file (overwrite it)Prisma Schema
npx prisma migrate dev
and enter a name for the migrationnpm run dev
A few notes.
interactiveTransactions
)FOR QUICK REFERENCE
Prisma Schema
SQL Script