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
How to use raw() in returning? #2571
Comments
You are using it correctly (except from using ?? bindings for quoting column names) and since output of the queries is the same they do return the same result. You have some other problem in your code, please provide complete reproduction code for your problem (with db initialization etc.). https://github.com/tgriesser/knex/blob/master/CONTRIBUTING.md#what-is-minimal-code-to-reproduce-bug-and-why-i-have-to-provide-that-when-i-can-just-tell-whats-the-problem-is |
My
Here is the test file: const { createHash, createSalt, head, prop } = require('lib/utils')
const db = require('knex')({
client: 'pg',
connection: {
host: process.env.PGHOST,
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE,
port: process.env.PGPORT
},
})
const salt = createSalt()
const user1 = {
username: 'test',
hash: createHash('pwd', salt),
salt,
type: 1,
store: null,
contact: 1,
}
const user2 = {
username: 'test2',
hash: createHash('pwd', salt),
salt,
type: 1,
store: null,
contact: 1,
}
const s = async () => {
console.log(db('users')
.insert(user1)
.returning(db.raw(`??`, [
[
'id',
'username',
'type',
'locked',
]
])).toString())
const res1 = await db('users')
.insert(user1)
.returning(db.raw(`??`, [
[
'id',
'username',
'type',
'locked',
]
]))
console.log('Result for first one:', res1);
console.log(db('users')
.insert(user2)
.returning([
'id',
'username',
'type',
'locked',
]).toString());
const res2 = await db('users')
.insert(user2)
.returning([
'id',
'username',
'type',
'locked',
])
console.log('Result for second one:', res2)
}
s().then(console.log).catch(console.error) That is the output of console:
|
@qwang07 thanks, this test case seems really interesting. Try to print queries with |
The code is as same as above except
|
If anyone has time to debug and see whats happening inside knex that is causing that help would be very welcome. I don't have time to start debugging this right now. |
In 0.14.6 I got the same insert into "users" ("contact", "hash", "salt", "store", "type", "username") values (1, 'asffasasf', 'asdsasdasda', NULL, 1, 'test') returning "id", "username", "type", "locked"
Result for first one: [ 7 ]
insert into "users" ("contact", "hash", "salt", "store", "type", "username") values (1, 'asdasddassda', 'asdsasdasda', NULL, 1, 'test2') returning "id", "username", "type", "locked"
Result for second one: [ anonymous { id: 8, username: 'test2', type: 1, locked: null } ] The thing here is that calling any of As can be seen here it does not and cannot handle when I think there are two ways of handling this:
I like the first option better. That way no matter how the So var returns = [];
for (var i = 0, l = resp.rows.length; i < l; i++) {
var row = resp.rows[i];
if (returning === '*' || Array.isArray(returning)) {
returns[i] = row;
} else {
// Pluck the only column in the row.
returns[i] = row[(0, _keys2.default)(row)[0]];
}
} Simply turns into var returns = [];
for (var i = 0, l = resp.rows.length; i < l; i++) {
var row = resp.rows[i];
returns[i] = typeof returning === 'string' ? row[returning] : row;
} I haven't tested this so maybe I'm missing some edge-case, but essentially whatever is returned from the database is what would be added to the response. |
@tgriesser What's your take on this? Do you see any reason why knex should be looking at the returning value from builder once the response from SQL has been received? My mindset is always postgres, so maybe I'm missing something relating to other dialects. But I would like to think whatever was returned from database using builder's returning value should be correct without knex doing any extra checks.. |
Hello there. I came here because I searched about an open "good first issue, " and it looks like there is a Pull Request merged. So, this issue is still open or it should be marked as closed/solved? |
I've done it like that:
|
Environment
Knex version: 0.14.6
Database + version: PostgreSQL 10.0
OS: Debian 9
How should I use
knex.raw()
inreturning()
function? The output of those two statements are the same:However, the first one can return the data correctly, the second will return
[ undefined ]
. Because I want to use SQL functionconcat_ws()
in returning statement, I have to useraw()
to create the query.The text was updated successfully, but these errors were encountered: