loopbackio / loopback-connector-postgresql

PostgreSQL connector for LoopBack.
Other
117 stars 181 forks source link

Numeric type doesn't handle floating point #33

Closed morenoh149 closed 9 years ago

morenoh149 commented 9 years ago

The mapping of a floating point number is to Postgres's NUMERIC type. This causes a runtime error. What should be done about this? I see loopback wants to expose this interface http://docs.strongloop.com/display/LB/LoopBack+types stating the a floating point number can be handled by that numeric type. This connector then must detect if the number passed in is a floating point or integer and create the apporpriate pg type. correct?

BerkeleyTrue commented 9 years ago

Here is an example error given when using the explorer to create data.

// model definition relative property.
    "matLat": {
      "type": "number",
      "postgresql": {
        "columnName": "mat-lat",
        "dataType": "integer"
      }
    }

// Data 
{"matId":"d4", "matLng": 37.11}

// Error
{
  "error": {
    "name": "error",
    "status": 500,
    "message": "invalid input syntax for integer: \"37.11\"",
    "length": 90,
    "severity": "ERROR",
    "code": "22P02",
    "file": "numutils.c",
    "line": "104",
    "routine": "pg_atoi",
    "stack": "error: invalid input syntax for integer: \"37.1111\"\n    at Connection.parseE (/Users/berkeley/node/loopback/gems/node_modules/loopback-connector-postgresql/node_modules/pg.js/lib/connection.js:534:11)\n    at Connection.parseMessage (/Users/berkeley/node/loopback/gems/node_modules/loopback-connector-postgresql/node_modules/pg.js/lib/connection.js:361:17)\n    at Socket.<anonymous> (/Users/berkeley/node/loopback/gems/node_modules/loopback-connector-postgresql/node_modules/pg.js/lib/connection.js:105:22)\n    at Socket.emit (events.js:95:17)\n    at Socket.<anonymous> (_stream_readable.js:764:14)\n    at Socket.emit (events.js:92:17)\n    at emitReadable_ (_stream_readable.js:426:10)\n    at emitReadable (_stream_readable.js:422:5)\n    at readableAddChunk (_stream_readable.js:165:9)\n    at Socket.Readable.push (_stream_readable.js:127:10)"
  }
}

changing the value to 37 works fine.

morenoh149 commented 9 years ago

woops nvm

raymondfeng commented 9 years ago

You can customize the DB type as follows:

{
  "rate": {
    "type": "number",
   "postgresql": {
     "dataType": "NUMERIC(10,2)"
   }
  }
}
BerkeleyTrue commented 9 years ago

This issue arose in confusion over the docs. loopback to postrgresql types states that loopback maps number to integer. Works fine if you map the property to DECIMAL instead.