knex.js

How to get a list of all updated records in knex / mysql

允我心安 提交于 2019-12-11 00:35:18
问题 Here's the query I'm working on: return knex('table') .returning('id') .where('boolean', false) .andWhere('fooID', foo.id) .update({ boolean : true }) .limit(num) .then(function(ids) { console.log('\nids'); console.log(ids); //outputs num ids now contains 3, which is the number of affected rows. Is there any way to get the ids of those 3 rows? I was under the impression .returning() did that, but it appears to not. 回答1: Mysql database doesn't support returning statement and it returns just

How to add multiple rows using “Insert … ON DUPLICATE KEY UPDATE” using knex

て烟熏妆下的殇ゞ 提交于 2019-12-10 11:22:40
问题 so I've been playing around with knex lately, however I found myself on a situation where I don't know what to do anymore. so I have this query: knex.raw("INSERT INTO tablename (`col1`, `col2`, `col3`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE col2 = VALUES(`col2`)", [ ['val1', 'hello', 'world'], ['val2', 'ohayo', 'minasan'], ]); And for some reasons It throws me an error Expected 2 bindings, saw 3 . I tried making it: knex.raw("INSERT INTO tablename (`col1`, `col2`, `col3`) VALUES (?, ?, ?)

Knex Migration Postgres Heroku - Error: Unable to Acquire Connection

时间秒杀一切 提交于 2019-12-10 10:18:16
问题 I am trying to run my first migration which creates a single table in a heroku postgres database. When I try to run knex migrate:latest --env development I receive the error Error: Unable to acquire a connection . Things I've tried: adding ?ssl=true to the end of my connection string stored in process.env.LISTINGS_DB_URL as I'm aware this is sometimes a requirement to connect with heroku setting the env variable PGSSLMODE=require I also stumbled across this article where someone has commented

How to do knex.js migrations?

旧时模样 提交于 2019-12-09 04:18:19
问题 I'm still not sure how to do my migrations with knex. Here is what I have so far. It works on up , but down gives me FK constraint error even though foreign_key_checks = 0. exports.up = function(knex, Promise) { return Promise.all([ knex.raw('SET foreign_key_checks = 0;'), /* CREATE Member table */ knex.schema.createTable('Member', function (table) { table.bigIncrements('id').primary().unsigned(); table.string('email',50); table.string('password'); /* CREATE FKS */ table.bigInteger(

Knex.js multiple orderBy() columns

谁都会走 提交于 2019-12-08 16:02:54
问题 Is it possible to do multiple orderBy() columns? knex .select() .table('products') .orderBy('id', 'asc') The orderBy() chainable only takes a single column key and a sort value, but how can I order by multiple columns? 回答1: You can call .orderBy multiple times to order by multiple columns: knex .select() .table('products') .orderBy('name', 'desc') .orderBy('id', 'asc') 回答2: The original answer is technically correct, and useful, but my intention was to find a way to programatically apply the

With knexjs, how do I compare two columns in the .where() function?

浪子不回头ぞ 提交于 2019-12-08 15:26:15
问题 Using knexjs only (no bookshelf) I would like to do something like the following query: select * from table1 where column1 < column2 However, when I do this: .table("table1").select().where("column1", "<", "column2") The SQL that knexjs generates is: select * from table1 where column1 < 'column2' Which doesn't give the desired result b/c it's not comparing the value from the column, it's comparing the value of the string, 'column2'. Anyone know how to do what I'm wanting? Thanks! 回答1: Ok, so

query multiple tables with knex.js

主宰稳场 提交于 2019-12-08 03:28:10
问题 I want to render with Expres.js and knex.js two tables using for that only one get function in order to use the data from both tables in one HTML template. It works when I query only one table (schools or students) but I don't know how to do with two tables. Any suggestion? app.get('/schools', function(req, res) { knex.select() .from('schools', 'students') .then(function(schools, students) { res.render('schools', { schools: schools, students: students }); }).catch(function(error) { console

How do you chain queries in order using knex.js?

馋奶兔 提交于 2019-12-07 01:31:05
问题 I'm having some trouble understanding how the promises in Knex.js work (uses Bluebird.js for promises). I'm trying to do something pretty simple, execute different insert statements one after another in order, but I haven't been able to get it to work. Here's the code I have so far, which is meant to execute an insert on the authentication_type table, then an insert on the user_table, and then an insert on the category table. // Import database connection var knex = require('./db-connection

Sub-query in Knex

吃可爱长大的小学妹 提交于 2019-12-06 18:47:29
问题 I'm looking to essentially make this sort of query in Knex, but I can't quite get it to work: select distinct * from ( select *, 1 as rank from table1 where Word like 'mike' union select *, 2 as rank from table1 where Word like 'mike%' union select *, 3 as rank from table1 where Word like '%mike%' ) as X order by WordOrder I noticed a similar issue here and tried to follow their advice, but can't seem to spot my bug (or if this is even the proper way of doing this in the first place). var q =

query multiple tables with knex.js

允我心安 提交于 2019-12-06 14:59:45
I want to render with Expres.js and knex.js two tables using for that only one get function in order to use the data from both tables in one HTML template. It works when I query only one table (schools or students) but I don't know how to do with two tables. Any suggestion? app.get('/schools', function(req, res) { knex.select() .from('schools', 'students') .then(function(schools, students) { res.render('schools', { schools: schools, students: students }); }).catch(function(error) { console.log(error); }); }); your approach indeed works, however i suggest you this idiom in order to avoid the