knex / knex

A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.
https://knexjs.org/
MIT License
19.16k stars 2.11k forks source link

Strange Behaviuor when doing an insert #31

Closed arturocandela closed 11 years ago

arturocandela commented 11 years ago

I've got a problem when I do an insert, but I do the same think in an update and it works perfectly.

I will try to explain: I'm using, as you suggested me, the Knex.Raw() to run Postgre functions for the updates and the inserts but I have problems with the insert, but not with the update: if i try to add the following js I get an error:

brigadista.nombre = "DarthVader";
 brigadista.posicion = 'ST_Transform(ST_GeometryFromText(\'POINT(-0.475519999981581 38.69480999998364 600)\',4326),32630)'
 brigadista.alturaabsoluta = false;

I get the error:

{ nombre: 'DarthVader',
  posicion: 
   { value: 'ST_Transform(ST_GeometryFromText(\'POINT(-0.475519999981581 38.69480999998364 600)\',4326),32630)',
     client: 
      { name: 'main',
        debug: true,
        connectionSettings: [Object],
        grammar: [Object],
        schemaGrammar: [Object],
        pool: [Object] } },
  alturaabsoluta: false }
{ sql: 'insert into "brigadista" ("alturaabsoluta", "nombre", "posicion") values ($1, ST_Transform(ST_GeometryFromText(\'POINT(-0.475519999981581 38.69480999998364 600)\',4326),32630), $2) returning "id"',
  bindings: [ false, 'DarthVader' ],
  __cid: '__cid2' }

But if I send:

brigadista.alturaabsoluta = false; brigadista.nombre = "DarthVader"; brigadista.posicion = 'ST_Transform(ST_GeometryFromText(\'POINT(-0.475519999981581 38.69480999998364 600)\',4326),32630)'

{ alturaabsoluta: false,
  nombre: 'DarthVader',
  posicion: 
   { value: 'ST_Transform(ST_GeometryFromText(\'POINT(-0.475519999981581 38.69480999998364 600)\',4326),32630)',
     client: 
      { name: 'main',
        debug: true,
        connectionSettings: [Object],
        grammar: [Object],
        schemaGrammar: [Object],
        pool: [Object] } } }
{ sql: 'insert into "brigadista" ("alturaabsoluta", "nombre", "posicion") values ($1, $2, ST_Transform(ST_GeometryFromText(\'POINT(-0.475519999981581 38.69480999998364 600)\',4326),32630)) returning "id"',
  bindings: [ false, 'DarthVader' ],
  __cid: '__cid2' }

I think the error is because the properties are sorted but not the values. I'm sorry but I don't have enough time to test this week if the error occurs without Knex.Raw data.

tgriesser commented 11 years ago

I think it has to do with the fact that the value sorting isn't taking into account the Raw instances. I'll take a look.

tgriesser commented 11 years ago

@arturocandela what version of Knex are you seeing this issue with?

Edit: Nevermind, I was able to reproduce it.

tgriesser commented 11 years ago

This is fixed and should be good to go on the 0.2 release I just cut. Thanks for pointing it out!

arturocandela commented 11 years ago

Thanks for all! Tomorrow,

If I had time I will upgrade my knex version and test it

2013/8/7 Tim Griesser notifications@github.com

This is fixed and should be good to go on the 0.2 release I just cut. Thanks for pointing it out!

— Reply to this email directly or view it on GitHubhttps://github.com/tgriesser/knex/issues/31#issuecomment-22264251 .

tgriesser commented 11 years ago

Great, change log is here... http://knexjs.org/#changelog

arturocandela commented 11 years ago

I have had time and I tested it and works perfectly :)

This is the result:

 sql: 'insert into "brigadista" ("alturaabsoluta", "nombre", "posicion") values ($1, $2, ST_Transform(ST_GeometryFromText(\'POINT(-0.475519999981581 38.69480999998364 600)\',4326),32630))',
  bindings: [ false, 'Han Solo' ],
  __cid: '__cid14' }