-
-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relax check on missing keys when doing a multi insert. #268
Comments
One issue to consider here is that if I relax this check, because the vector-of-maps is turned into a vector-of-column-names and a sequence of vector-of-row data, any hash maps that are missing columns will get explicit Some databases allow Do you know if your database(s) support Concrete example: (sql/insert-multi! ds :mytable [{:a 1} {:a 2 :b 3}])`
;;=>
`INSERT INTO mytable (a, b) VALUES (1, DEFAULT), (2, 3)` That's much harder to do and will not work with batch inserts, so I'd rather not go down that path (this is something that HoneySQL supports for non-batch inserts, BTW, so I'm beginning to lean toward not implementing this at all within |
Hi Sean, Both the databases I'm most familiar with, PostgreSQL and MySQL (and I suppose by extension MariaDB) all support using I guess my feeling is that it is the responsibility of the programmer to deal with any (multi) insertions that don't include all the keys (perhaps they were either stripped out during a previous filter step, or not even supplied for reasons) and to figure out what the best course of action is to take (either ensure the keys are there, or ensure there are defaults in the columns). If an insert is attempted and a column is not nullable and the key (and value) are missing, the database would throw an error and I would hope the programmer would catch that during their testing and do the appropriate thing. In my case, for the moment, I'm doing a default-with-merge to work around the all-keys must be present check in next.jdbc, e.g.,
Where it's possible, due to some logic beforehand that I would side with your reasoning that next.jdbc shouldn't do anything fancy. |
That's not what I'm asking. Read the SQL code example that uses
True. And I'm considering going that far -- I'm just giving the caveat that for missing columns in a particular row's hash map, it will insert I think that's a niche use case but it does exist. At this point, I think I would want to make the |
Hi, I'm familiar with the DEFAULT, perhaps I worded my reply incorrectly:
Having an opt-in for multi-insert might be a good way forward as you suggest. -=david=- |
I've spent some more time looking at c.j.j's behavior and I realized that the "sequence of hash maps" behavior in c.j.j is not like In c.j.j, if you use In c.j.j, if you use Here's the docstring of c.j.j's
So, a "compatibility mode" here could detect the hash maps not all having the same keys and potentially resort to a sequence of single-row It looks like you've already gone for the former approach -- which provides the batch insert performance (which you were not getting with c.j.j). Thoughts? |
Hi, Thank you for the investigation. I follow what you're saying up to the last point - are you considering having an opt-in if the programmer knows (in advance) that sometimes their maps may contain missing keys? If so, then I think that's totally fine - with (of course) documentation around the behaviour of All good stuff! Thank you indeed. -=david=- |
"I think I'd rather push that trade-off directly onto the programmer tho': making them either explicitly augment the rows so they all match, or explicitly doing row-by-row In other words, keep the behavior exactly as-is but clearly document that I don't think it's a good idea to offer an option that can change fast, bulk inserts into slow, sequential inserts silently, just because you have one or more hash maps in a sequence that does not have the same keys as all the others. Throwing an exception, and forcing the programmer to "do the right thing" is a safer approach here. |
Sounds good to me! :-) |
That commit is the documentation updates, so you can see what I changed. |
Hi,
Using next.jdbc 1.3.909 on Clojure 1.11.1.
In next.jdbc, for
insert-multi!
, at these lines, there is a check to determine if the map keys are all the same before doing an insert. If they are not all the same (or if some keys are missing), an exception is thrown.This is different from how
clojure.java.jdbc
handles it - in the sense thatclojure.java.jdbc
allows for multi-inserts even if the keys are different (or missing).I believe this check in next.jdbc is too strict and should be relaxed, for it is perfectly acceptable to have a collection of maps for insertion, with some of the map keys missing (due to previous logic in the application), knowing that certain columns in a table permit null's or default to known values, thus the absence of keys shouldn't cause an exception (perhaps let the database throw the exception instead, if an attempt is made to insert a row with missing values - as it's the source of truth :-) )
Please could you consider removing this check in next.jdbc to permit maps to having missing or different keys.
Thank you.
-=david=-
The text was updated successfully, but these errors were encountered: