-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
PostgreSQL adds quotes around number literals. #1001
Comments
I believe since there is no ambiguity in interpreting numbers as columns vs values, there is no problem with either approach. |
I was actually surprised by this, but it looks like the query plans are identical for each case also: CREATE TABLE test (
id serial NOT NULL PRIMARY KEY,
field integer NOT NULL DEFAULT 0
);
INSERT INTO test (field) VALUES (1), (2), (3), (4); First query: EXPLAIN SELECT * FROM test WHERE field = '4'; First query results:
Second query: EXPLAIN SELECT * FROM test WHERE field = 4; Second query results:
The main thing to note is that the I think the correct behavior is to not use quotes, but it's good to know that the quotes aren't actually doing any harm. |
@chrisbroome thanks heaps for looking into that.
Agreed. I'm going to leave this open for now in case someone has time to provide a PR or can shed some light on why it's as it is. |
[0] https://wiki.postgresql.org/wiki/Things_to_find_out_about_when_moving_from_MySQL_to_PostgreSQL |
So this may not be an issue for For Example:
Yields
|
Just ran into this exact problem, I used |
Nice! Thanks |
As far as I can tell, this applies to all ways of running queries via knex. By debugging, I found that it's the pg library doing this js conversion. Reference: https://github.com/brianc/node-postgres/blob/master/lib/utils.js#L46 Correct me if I'm wrong. |
FWIW, in my case, when using node-postgres directly, the query gets generated properly and numeric columns are not being quoted.... |
queries that contains arithmetic operations will most likely fail using quotes. At least It seems like the last value of generate_series is not accepting a string. |
FWIW, I'd like it to not use the quotes there. Using quotes is simply not something the developer using that actually expects to happen, and there are always edge-cases where this causes code not to work as intended. The fact that in most cases it does not matter whether we quote the values or not is just not a good enough reason to do the quoting, as I'd very much prefer an infrastructure level lib like I am ok with libs not being smart enough and doing literally what I tell them, but being too-smart and doing things I did not tell them explicitly to do and ending up breaking or achieving a different effect from the one intended is super annoying. 😆 |
I tried this once more - for postgres - by removing knex's number -> string conversion, but again this does not affect the final query since as described above, the So the question remains if this is really a 'bug' and if it's something we should try to handle in knex. |
how remove quotes I find string type id where cause
but remove quotes there is no errors
->
|
I experience the same issue with knex.raw('substring(??, ?)', ['path', 5]) 🟢 works knex.raw('substring(??, ?::integer)', ['path', 5]) |
Adding an issue here because I don't know what the correct behaviour is, but I noticed that PostgreSQL queries seem to add quotes around numbers.
Is this to handle a use case I'm not familiar with? Or is it a bug in Knex's postgres code?
The text was updated successfully, but these errors were encountered: