"Calculate via subquery on insert" properties on models contribution proposal #39663
Unanswered
askvortsov1
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone! Had a few questions about a potential feature request / contribution.
I'm a core developer on the Flarum forum project, we use the Eloquent package for our DB layer. One of our models represents a "post" in a discussion, and has a
number
field. This (coupled with the discussion ID) is intended to represent a permalink to the post, as well as the index of the post in the discussion.We've been running into an issue on large communities with many distributed nodes where multiple users attempt to post to the same discussion at the same time, and as a result, there is an attempt to save two posts with the same number in the same discussion. This fails due to DB uniqueness constraints, and occurs because the number for the new post is pulled non-atomically before saving. To mitigate this, we'd like to generate the post number on the DB level. We came across the Eloquent query builder's
insertUsing
method, which seems like a great fit for this. It seems that we could add this to our model in question by overriding thesave
method but that's a bit messy, and we also felt that this type of functionality might be useful to other Laravel users too.With this in mind, I would like to propose adding a
$calculateViaSubqueryOnInsert
attribute to the Eloquent Model, which would represent a map of attribute names to callbacks used to calculate that attribute (with the query builder and model instance as arguments). I would also modify theperformInsert
method onModel
to use this when appropriate.Is this a contribution you would be interested in having? If so, one challenge I envision is combining
insertUsing
andinsertAndSetId
: I'm not sure if it'd be preferable to have a new method (insertAndSetIdUsing
), or if there's a better way to compose the two?Beta Was this translation helpful? Give feedback.
All reactions