This is a question about Ecto’s
prepare_query/3 callback. Specifically, on the fact that it only works for the “Query API” (
aggregate and friends).
I can’t find any detailed information on why it doesn’t support the “Schema API”, which mostly contains write operations.
I’ve found a couple of GitHub issues (issues/3608 and issues/3702) where this was briefly discussed, but nothing was said on the why. I’ve also found this old topic here on the forum, which however didn’t receive any replies.
At the moment the Query API contains mostly read operations, with two exceptions:
update_all/3, which support
At the same time, the Schema API contains
update/2, which do not support
I suspect that the reason is that the Query API works on free-form queries composed with the Ecto DSL, where as the Schema API works on concrete structs that represent a DB row (that is, a schema). I can see the point that allowing applications to modify SQL that is intended to work on a predefined struct could be confusing. If the API promises that you’ll be inserting data for a struct, then if that SQL gets modified on the fly that promise is broken. This my just my guess: I couldn’t find it documented or explained anywhere.
And yet, I would argue that this is an inconsistency that we don’t need. Following the same line of reasoning, the Query API promises the developer that a certain query will be performed, but that query could be modified on the fly by the application using
prepare_query/3. That’s also a promise being broken, and that’s fine. It’s a useful feature.
So, my questions are: is there is any reason why we can’t have an API to prepare queries for write operations? Can it be added?