waterthetrees / wtt_server

Water The Trees Postgres/Express/Node.js backend
https://waterthetrees.com
Creative Commons Zero v1.0 Universal
0 stars 3 forks source link

createIdForTree causes "integer out of range" error on insert #84

Closed jazhen closed 2 years ago

jazhen commented 2 years ago

Context

Attempting to add a new tree fails with an "integer out of range" error. This is due to the createIdForTree function generating ids that are larger the int maximum in Postgres (i.e. -2147483648 to +2147483647). I was able to verify this by hard coding the inserted id. When the id was 2147483647 (i.e. the int max), the insert was successful. However, when the id was 2147483648 (i.e. 1 + the int max). the insert failed.

Recreate the Issue

Sending a POST request to localhost:3002/api/trees when a body of

{
  "city": "Montara",
  "common": "Oregon Crab Apple",
  "scientific": "Malus fusca",
  "genus": "Malus",
  "datePlanted": "2022-02-22",
  "dbh": "",
  "height": "",
  "address": "1208 Alamo St",
  "state": "CA",
  "zip": "94037",
  "neighborhood": "",
  "lng": -122.48007074505206,
  "lat": 37.63264661737338,
  "owner": "public",
  "who": "",
  "volunteer": "jasonzhen.mail",
  "notes": "",
  "health": "good",
  "email": "jasonzhen.mail@gmail.com",
  "idReference": "WTT202202227265902"
}

generates an id of 886478645782219 and has a response of

{
  "error": "integer out of range"
}

Possible Solution

We can change the treedata.id column to bigint type, which has a range of -9223372036854775808 to +9223372036854775807.

This resource proposes one method of altering the column type.

Screenshot

image

fwextensions commented 2 years ago

Why not just store it as a string? It's just an opaque unique identifier, and doesn't need to be treated as an int once it's created, as I understand it.