skip to Main Content

I’m encountering an issue while trying to insert an array of strings into a PostgreSQL database column. The column is defined as follows:

path TEXT[] NOT NULL

here is the schema:

path: { type: 'array', items: { type: 'string' }, notNull: true }

here I send the format of path like this

await updateGlobalChatNotificationOptIn({
      variables: {
        path: ['chat'],
        option: updatedGlobalChatNotificationOptIn,
      },

updateNotificationOption(path: [String!]!, option: String!): NotificationUserOption

when I pass the path something like this ['chat']


  // If no existing record, create a new one
      return NotificationUserOption.query().upsertGraphAndFetch(
        { userId, path, groupId, option },
        { insertMissing: true },
      )
    },
    
  extend type Mutation {
    updateNotificationOption(path: [String!]!, option: String!): NotificationUserOption
  }

However, when I try to insert the array using this code, I encounter the following error:

malformed array literal: "["chat"]"
"[" must introduce explicitly-specified array dimensions.

I’ve tried using single brackets ('{chat}') and double brackets ([['chat']]), but neither seems to work.

How can I correctly format the array literal so that I can insert the array into the PostgreSQL column without encountering this error?

2

Answers


  1. To insert the array of strings into a PostgreSQL column of type TEXT[],
    you need to convert the JavaScript array into a string representation using curly braces and double quotes for each element.

    Do the following in Objection.js:

    // Convert the array to a string representation
    const pathArray = ['chat'];
    const pathString = `{${pathArray.join(',')}}`;
    
    // Insert the data into the database using Objection.js
    await NotificationUserOption.query().upsertGraphAndFetch(
      { userId: ctx.user, path: pathString, groupId, option },
      { insertMissing: true }
    );
    

    Now TEXT[] will receive the data without any "malformed array literal" errors. Objection.js will handle the conversion back to a JavaScript array when querying the data from the database.

    #Apache-Age #posgresql #graphql #objection.js

    Login or Signup to reply.
  2. I guess you are passing the path as a string, but the database expects it as an array.
    Pass the path as an actual JavaScript array, and i hope Objection.js will handle the conversion to the correct format for the PostgreSQL column.

    Make changes as follows:

    const pathArray = ['chat']; // Pass path as a JavaScript array
    const option = updatedGlobalChatNotificationOptIn ? '30MinSummary' : 'off';
    
    await updateGlobalChatNotificationOptIn({
      variables: {
        path: pathArray, // Pass the array directly
        option: option,
      },
    });
    

    and then Objection.js function:

    await NotificationUserOption.query().upsertGraphAndFetch(
      { userId: ctx.user, path, groupId, option },
      { insertMissing: true }
    );
    
    

    It should resolve the error you facing..

    #Apache-Age #postgresql #graphql

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search