Skip to content

[PostgreSQL] Array of ENUMS returned sometimes as String instead of array #15466

Open

Description

@fegauthier

Issue Creation Checklist

  • [ X] I understand that my issue will be automatically closed if I don't fill in the requested information
  • [ X] I have read the contribution guidelines

Bug Description

Hi guys! I have some issues with sequelize with Postgresql when I'm fetching rows that contains Array of ENUMS. Most of the times, Array is returned but sometimes a String is returned instead of an array.

Reproducible Example

It's hard to reproduce as it's random.

There is my model:

module.exports = (sequelize, DataTypes) => {
    const User = sequelize.define('User', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
        },
        firstName: DataTypes.STRING,
        lastName: DataTypes.STRING,
        username: {
            type: DataTypes.STRING,
            unique: true,
        },
        password: DataTypes.STRING,
        email: DataTypes.STRING,
        roles: DataTypes.ARRAY(DataTypes.ENUM('ADMIN', 'CLIENT')),
        isActive: DataTypes.BOOLEAN,
        locale: {
            type: DataTypes.STRING,
            defaultValue: 'fr',
        },
    }, {});

    User.associate = (models) => {
        models.User.hasMany(models.Token);
    };

    return User;
};

Code used to findOne an user

const user = await models.User.findOne({
    attributes: ['id', 'firstName', 'lastName', 'username', 'email', 'isActive', 'roles', 'locale'],
    where: {
        id: userId,
    },
});

There is another issue that was closed.

#14329

There is also an issue dated from 2017 on PG github repo

brianc/node-pg-types#56

What do you expect to happen?

Array of ENUMS should always be returned as an Array and not a String.

Return should always be

['ADMIN', 'CLIENT']

and not

{ADMIN,CLIENT}

What is actually happening?

Most of the times, Array is returned but sometimes a String is returned instead of an array.

Environment

  • Sequelize version: latest
  • Node.js version: 16
  • Database & Version: Postgresql 14
  • Connector library & Version: pg 8.7.3

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. added
    pending-approval
    type: bug
    on Dec 16, 2022
  2. ephys commented on Dec 16, 2022

    @ephys
    Member

    Unfortunately just like with #14329, I've ran a few different tests around 100k times each, and I'm completely unable to reproduce this issue :(

    Each enum in postgres is its own DataType, and in Sequelize 6, Array Data Type parsers must be registered before the query executes (it's done automatically by sequelize, it's handled by this code). That is almost certainly what's failing. The thing is that the list of postgres types is fetched when the first connection is created, and is updated every time sequelize creates an enum. While imperfect, I don't see a scenario where the type parser map would be outdated

    That logic has been completely rewritten in v7 and takes a different approach. It's possible (but since I don't know how to reproduce the issue I cannot be sure) that the problem has been fixed in v7

    Without figuring out how to reproduce this bug, I can't really provide more help than this

  3. github-actions commented on Dec 31, 2022

    @github-actions
    Contributor

    This bug report has not been verified by maintainers yet, and has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, consider commenting with more information to help us verify it.

  4. gustavo-acr commented on Mar 21, 2023

    @gustavo-acr

    I reproduced this bug

    • I created an enum in PostgreSQL
    • I defined a column in PostgreSQL as an array of this enum
    • I defined the model of the field as
      type: DataTypes.ARRAY(DataTypes.ENUM(...Object.values(ContactSectionInfo))),
      It sends a string instead of an array on the prod, on dev environment it works well

    Version sequelize: 6.29.0.

  5. WikiRik commented on Jun 28, 2023

    @WikiRik
    Member

    @gustavo-acr that is works on dev, but not prod does not give me confidence that this is an issue with sequelize. But you can create a SSCCE and if that can reproduce the issue we can look further into this. But as was mentioned before, it is likely fixed with the DataTypes rewrite in v7

  6. shekvl commented on Apr 22, 2024

    @shekvl

    I noticed one situation. I have a node.js server which accesses a PostgreSQL database by means of sequelize. In the database, some tables contain array columns with ENUM array elements:

    CREATE TYPE public.enum_collections_type AS ENUM ( 'BIRTH_COHORT', 'CASE_CONTROL', ... );

    CREATE TABLE test ( ... type public.enum_collections_type[] NOT NULL, ... );

    These columns are mapped like that:

    type: { type: DataTypes.ARRAY(DataTypes.ENUM(CollectionTypes)), allowNull: false, validate: {len: [1, undefined]} },

    This setup works, the data is returned as arrays:

    type: ['BIRTH_COHORT', 'CASE_CONTROL' ... ]

    Recently I was forced to drop the database and recreate it from the dump which contained the same schema and almost the same data. During this operation, the server was not stopped. After this, the data for all such columns started to be returned as strings:

    type: '{BIRTH_COHORT, CASE_CONTROL ...}'

    After restarting the server, the output became correct again. I was able to reproduce that, i.e. every time after dropping and recreating the database the strings started to be returned, and after restarting the server the arrays started to be returned again. Upgrading to [email protected] did not change anything.

  7. ephys commented on Apr 23, 2024

    @ephys
    Member

    That would make sense as the list of OIDs would not match the database anymore

    I guess that could also be true if the user attempts to connect to multiple different databases that are not perfectly replicated, the custom types could have different OIDs

    I'll reopen to see what can be done about this

  8. reopened this on Apr 23, 2024
  9. removed
    pending-approval
    on Apr 23, 2024
  10. alexDevBR commented on Dec 3, 2024

    @alexDevBR

    I found how to reproduce this for the case when restarting the client doesn't help. The bug happens when the oid of the enum is larger than 2^31. parseField() here is using reader.int32, when it should be reading an unsigned int. I'm pretty sure tableID should also be an uint32.

  11. added theissue type on Mar 9, 2025
  12. removed
    type: bug
    on Mar 9, 2025
  13. pyrocks commented on Mar 20, 2025

    @pyrocks

    Hi, seeing

    which contains the fix, I assume it will be resolved in the next release?
    when can we expect it?
    thanks

  14. WikiRik commented on Mar 20, 2025

    @WikiRik
    Member

    Using alpha 45, you can already update to the latest pg version (it's pinned with a ^). In alpha 46, the minimum version of pg will be the updated one. I think we will release that one next week

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

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions