mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Implement a ReturningIdGenerator that leverage INSERT INTO .... RETURNING id
#7243
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @allan-simon on GitHub (Nov 4, 2023).
Feature Request
Summary
Add in
AbstractIdGeneratora methodwhich will return false by default
Note: a "cleaner" way would have been to replace isPostInsertGenerator by a "getGeneratorKind" that return an enum pre/during/post but that would break BC
create a
ReturningIdGenerator(I'm bad at naming) that will havefunction isInsertReturningGenerator { return true;}so that after in
BasicEntityPersistercan be modified to havegetInsertSQLto add aRETURNING %idColumnand then in
executeInsertswe can dothat will divide by two the number of request required when doing inserts (as you will not need the pre or post insert request to get the id )
especially as this keyword is supported by several vendors (potgresql , mariadb , oracle among others ) and it will be opt-in for the user.
If that seems like a good idea I may work on a PR
@awwar commented on GitHub (Nov 10, 2023):
I apologize for being off-topic, but this is a really cool idea and could be developed further.
During our work, we often encounter concurrency problems. I know that there are millions of ways to solve a problem when we try to:
... but 9 more processes are trying to insert poor Antony into the database.
1will succeed, but9will throw an exception and theirEntityManagerwill be closed. In such situations, me often enter the “OH MY LORD WHY IS IT SO HARD TO INSERT THIS ENTRY?” mode and rewrite everything into pure sql.I use this query:
As a result, I will either insert a record or get an existing one. (Then I do a few dirty tricks so that the original entity changes the data to the ones in the database, but does not trigger the update on the next
->flush())It would be very cool if there was some kind of
#[IdempotentEntity(conflict: 'index, index2')]attribute that, during->flush(), would turn all insert requests for this entity into the query I indicated above. Of course this whole thing is optional.@FluffyDiscord commented on GitHub (Dec 29, 2023):
+1
@DAdq26 commented on GitHub (Jun 28, 2024):
+1
@DAdq26 commented on GitHub (Jun 28, 2024):
Mariadb example with timestamp is a really good one
https://mariadb.com/kb/en/insertreturning/
CREATE OR REPLACE TABLE t2 (id INT, animal VARCHAR(20), t TIMESTAMP);
INSERT INTO t2 (id) VALUES (2),(3) RETURNING id,t;
+------+---------------------+
| id | t |
+------+---------------------+
| 2 | 2021-04-28 00:59:32 |
| 3 | 2021-04-28 00:59:32 |
+------+---------------------+