Skip to content

Sequelize omitting field when casting to array #6572

Closed

Description

@SuperManifolds

I am attempting to use the postgres citext module with Sequelize, so far it has worked well by using literals for queries and defining the type in the model. However because Sequelize does not understand what the citext type is, it returns my citext arrays as strings. e;g "{item 1, item2}"
To get around this I am trying to cast the citext array to a TEXT or VARCHAR array in the query, however when I do this, Sequelize does not show that field at all.

If I set raw: true, the field shows fine, and as an array.

Users.findAndCountAll({
        where: { },
        attributes: [
          'id',
          'createdAt',
          'updatedAt',
          db.cast(db.col('nicknames'), 'text[]')
        ]
})

What do you expect to happen?

I expect there to be a nicknames field returned from sequelize and it to be an array, instead it's as if the attribute is not there at all.

What is actually happening?

This is the generated SQL, the SQL itself works fine when I try it in a database manager, but the output Sequelize gives me omits the nickname field.

SELECT "User"."id", "User"."createdAt", "User"."updatedAt", CAST("nicknames" AS TEXT[]) FROM "Users" AS "User" WHERE "User"."deletedAt" IS NULL;

Dialect: postgres
Database version: 9.5
Sequelize version: 3.23.4

Activity

  1. felixfbecker commented on Sep 9, 2016

    @felixfbecker
    Contributor

    You need to tell sequelize under which alias you want to have your expression. I believe the syntax is

    Users.findAndCountAll({
            where: { },
            attributes: [
              'id',
              'createdAt',
              'updatedAt',
              [db.cast(db.col('nicknames'), 'text[]'), 'nicknames']
            ]
    })

    but please refer to the docs

  2. SuperManifolds commented on Sep 10, 2016

    @SuperManifolds
    Author

    Worked! Can't believe I missed that, thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions