javascript - Knex.js insert from select -


i'm trying generate query following via knex.js:

insert table` ("column1", "column2") select "someval", 12345) not exists (     select 1     table     "column2" = 12345 ) 

basically, want insert values if particular value not exist. knex.js doesn't seem know how this; if call knex.insert() (with no values), generates "insert default values" query.

i tried following:

pg.insert()                 .into(tablename)                 .select(_.values(data))                 .wherenotexists(                     pg.select(1)                         .from(tablename)                         .where(blah)                 ); 

but still gives me default values thing. tried adding .columns(object.keys(data)) in hopes insert() honor that, no luck.

is possible generate query want knex, or have build raw query, without knex.js methods?

i believe select needs passed insert:

pg.insert(knex.select().from("tablenametoselectdatafrom")).into("tabletoinsertinto"); 

also in order select constant values or expressions you'll need use knex.raw expression in select:

knex.select(knex.raw("'someval',12345")).from("tablename") 

this first post , didn't test specific example i've done similar things you're asking using above techniques.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -