Different lastval() value with postgres trigger #7468

Closed
opened 2026-01-22 15:52:05 +01:00 by admin · 1 comment
Owner

Originally created by @matlow on GitHub (Jan 23, 2025).

Discussed in https://github.com/doctrine/orm/discussions/11803

Originally posted by matlow January 23, 2025

Summary

I have 2 entities : entityA which has a many to many relationship with entity B with join table named "tableA_tableB"
TableA and tableB use serial type on primary key.
When i persist new entity A, a new entity B is also persisted.
But i got an error if i had a postgresql trigger function on tableA to log insert operation.

detail 'An exception occurred while executing a query: SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "tableA_tableB" violates foreign key constraint "fk_d3b752a277570a4c"\nDETAIL: Key (tableA_id)=(18) is not present in table "tableA".'

There is the postgresql trigger :

create or replace function tableA_ait() returns trigger 
set schema 'public' language plpgsql as $$
begin
 	insert into log_audit (log_data, log_table, operation) values (row_to_json(new.*), 'tableA', TG_OP);	
 	return null;
end;
$$;

create trigger tableA_ait after insert on tableA
for each statement execute procedure tableA_ait();

Current behavior

Doctrine\ORM\Persisters\CollectionManyToManyPersister launch an exception because
Doctrine\ORM\Id\IdentityGenerator::generateId() return pdo_pgsql lastInsertId wich is he sequence of the log_audit table

Question
Is there a way to have the correct sequence table ?

Originally created by @matlow on GitHub (Jan 23, 2025). ### Discussed in https://github.com/doctrine/orm/discussions/11803 <div type='discussions-op-text'> <sup>Originally posted by **matlow** January 23, 2025</sup> **Summary** I have 2 entities : entityA which has a many to many relationship with entity B with join table named "tableA_tableB" TableA and tableB use serial type on primary key. When i persist new entity A, a new entity B is also persisted. But i got an error if i had a postgresql trigger function on tableA to log insert operation. > detail 'An exception occurred while executing a query: SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "tableA_tableB" violates foreign key constraint "fk_d3b752a277570a4c"\nDETAIL: Key (tableA_id)=(18) is not present in table "tableA".' There is the postgresql trigger : ```sql create or replace function tableA_ait() returns trigger set schema 'public' language plpgsql as $$ begin insert into log_audit (log_data, log_table, operation) values (row_to_json(new.*), 'tableA', TG_OP); return null; end; $$; create trigger tableA_ait after insert on tableA for each statement execute procedure tableA_ait(); ``` **Current behavior** `Doctrine\ORM\Persisters\CollectionManyToManyPersister `launch an exception because Doctrine\ORM\Id\IdentityGenerator::generateId() return pdo_pgsql lastInsertId wich is he sequence of the log_audit table **Question** Is there a way to have the correct sequence table ?
admin closed this issue 2026-01-22 15:52:05 +01:00
Author
Owner

@matlow commented on GitHub (Jan 29, 2025):

I drop the serial PK of log_audit table to fix it

@matlow commented on GitHub (Jan 29, 2025): I drop the serial PK of log_audit table to fix it
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7468