Skip to content

[Postgres] Array of enum is sometime returned as a string #14329

Closed

Description

@mi-mazouz

Bug Description

Hello everyone, I have the following models:

User.init(
  {
    firstname: DataTypes.STRING,
  },
  { sequelize, modelName: "user" }
);

Aailability.init(
  {
    userId: DataTypes.INTEGER,
    weekDays: DataTypes.ARRAY(DataTypes.ENUM('MONDAY', 'THUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')),
  },
  { sequelize, modelName: "availability" }
);

(async () => {
  await sequelize.sync({ force: true });

  const user = await user.create({
    firstname: "Mickael",
  });

  await availability.create({
    userId: user.id,
    weekDays: ['MONDAY']
  });

   const userAvailability = user.getAvailability();

  console.log(userAvailability);

  await sequelize.close();
})();

What do you expect to happen?

weekdays should come back as an array like:

{ weekDays: ['MONDAY'] }

What is actually happening?

Sometime it comes back as a string like:

{ weekDays: '{MONDAY}' }

Environment

  • Sequelize version: 6.6.2
  • Node.js version: 14.19.0
  • Database & Version: docker image, postgres:latest
  • Connector library & Version: pg 8.7.3, pg-hstore 2.3.3

Activity

  1. mi-mazouz commented on Apr 6, 2022

    @mi-mazouz
    Author

    My current workaround 😂

    get(this) {
      const weekDays = this.getDataValue('weekDays');
      
      if (isString(weekDays)) {
        return weekDays.replace('{', '').replace('}', '').split(',');
      } else {
        return weekDays;
      }
    }
    
  2. ephys commented on Apr 6, 2022

    @ephys
    Member

    Database & Version: docker image, postgres:latest

    For reference, at time of writing, that's postgres 14.2

  3. added
    type: bug
    on Apr 9, 2022
  4. ephys commented on May 20, 2022

    @ephys
    Member

    I've just tested this using the following code, but unfortunately was not able to replicate the issue. It was using Sequelize 6.19.2, postgres 14.3, pg 8.7.3, so the issue may already be fixed.

    If you can replicate this in 6.19.2 (or newer), could you post the exact code you used for it?

    class User extends Model {}
    User.init(
      {
        firstname: DataTypes.STRING,
      },
      { sequelize, modelName: "user" }
    );
    
    class Availability extends Model {}
    Availability.init(
      {
        userId: DataTypes.INTEGER,
        weekDays: DataTypes.ARRAY(DataTypes.ENUM('MONDAY', 'THUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY')),
      },
      { sequelize, modelName: "availability" }
    );
    
    User.hasOne(Availability);
    
    await sequelize.sync({ force: true });
    
    const user = await User.create({
      firstname: "Mickael",
    });
    
    await Availability.create({
      userId: user.id,
      weekDays: ['MONDAY']
    });
    
    const userAvailability = await user.getAvailability();
    
    console.log(userAvailability.weekday);
  5. removed
    type: bug
    on May 20, 2022
  6. github-actions commented on Jun 4, 2022

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it 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, just leave a comment or remove the "stale" label. 🙂

  7. fegauthier commented on Nov 25, 2022

    @fegauthier

    Hi guys!

    I have the same issue. It's random. Sometimes Sequelize return it as an Array and most of the time as a String.

    Console.log of User findOne

    User {
      dataValues: {
        id: '8a9ca273-70b9-471f-afbf-bf5b7ce874ef',
        firstName: 'Dev',
        lastName: 'Dev',
        username: 'dev',
        email: '[email protected]',
        isActive: true,
        roles: '{ADMIN,SITE_MANAGER}'
      },
      _previousDataValues: {
        id: '8a9ca273-70b9-471f-afbf-bf5b7ce874ef',
        firstName: 'Dev',
        lastName: 'Dev',
        username: 'dev',
        email: '[email protected]',
        isActive: true,
        roles: '{ADMIN,SITE_MANAGER}'
      },
      uniqno: 1,
      _changed: Set(0) {},
      _options: {
        isNewRecord: false,
        _schema: null,
        _schemaDelimiter: '',
        raw: true,
        attributes: [
          'id',
          'firstName',
          'lastName',
          'username',
          'email',
          'isActive',
          'roles'
        ]
      },
      isNewRecord: false
    }
    

    User 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', 'SITE_MANAGER')),
            isActive: DataTypes.BOOLEAN,
        }, {});
    
        User.associate = (models) => {
            models.User.hasMany(models.Token);
        };
    
        return User;
    };
    
    

    My sequelize version is 6.21.3
    My Postgres version is PostgreSQL 14
    My pg version is 8.7.3

  8. fegauthier commented on Dec 15, 2022

    @fegauthier

    Same issue with sequelize version 6.25.8

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