0

I have a table in postgres with a column called user_ids with its type set to integer[]

However, in defining the model for the table, I cannot seem to get it right, that when I try posting to the table, it always gives me an error. I have tried this:

 @property({
    type: "object",
    itemType: "number",
    postgresql: {
      dataType: "integer ARRAY"
    },
    name: 'user_ids'
  })
  userIds?: number[];

in which postgres throws the error: "message": "Unexpected number in JSON at position 109" when I post this as the body of the call:

"userIds": {
    1
  }

If I try this:

 @property({
    type: "object",
    itemType: "number",
    postgresql: {
      dataType: "integer ARRAY"
    },
    name: 'user_ids'
  })
  userIds?: number[];

then the database throws the error: malformed array literal: "[1]" when I put this in the body

"userIds": [
    1
  ]

Can someone tell me how to correctly define the model. I know postgres requires arrays to be in curly braces but no matter what I try, either loopback or postgres throws an error

Vikram Khemlani
  • 555
  • 6
  • 17

1 Answers1

2

For me @property({ type: 'array', itemType: 'number', postgresql: { dataType: 'integer ARRAY', }, }) did not work (tested 14 and 9.6 postresql), but it work for correct migration.

As mentioned in documentation for loopback-connector-postgresql there are only String[] supported array type. In source code of connector (lib/postgresql.js) there is method toColumnValue, and check for arrays const isArrayDataType = prop.postgresql && prop.postgresql.dataType === 'varchar[]';

So easiest way is fork and add your check smth like this

const isArrayDataType = prop.postgresql && (prop.postgresql.dataType === 'varchar[]' || prop.postgresql.dataType.includes('ARRAY'));

Dharman
  • 30,962
  • 25
  • 85
  • 135