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

3
  • I keep facing this issue as well. Have you found a solution? Commented May 14, 2020 at 11:16
  • Yea. I did this: @property({ type: 'array', itemType: 'number', postgresql: { dataType: 'integer ARRAY', }, }) userIds: number[] Commented Jun 11, 2020 at 9:32
  • @VikramKhemlani wouldn't this be specific to PostgreSQL and when DB is changed, this will no longer work. Commented Jan 23, 2021 at 10:39

1 Answer 1

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'));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.