Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequelize omitting field when casting to array #6572

Closed
SuperManifolds opened this issue Sep 8, 2016 · 2 comments
Closed

Sequelize omitting field when casting to array #6572

SuperManifolds opened this issue Sep 8, 2016 · 2 comments
Labels

Comments

@SuperManifolds
Copy link

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

@felixfbecker
Copy link
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

@SuperManifolds
Copy link
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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants