Repository navigation
Open
Description
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.
There is also an issue dated from 2017 on PG github repo
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.
Reacted by alex weinberger, Jonathan Stein and Patricio Marrone
Metadata
Metadata
Assignees
Labels
No labels
Activity
ephys commentedon Dec 16, 2022
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
github-actions commentedon Dec 31, 2022
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.
gustavo-acr commentedon Mar 21, 2023
I reproduced this bug
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.
WikiRik commentedon Jun 28, 2023
@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
shekvl commentedon Apr 22, 2024
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.
ephys commentedon Apr 23, 2024
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
alexDevBR commentedon Dec 3, 2024
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 usingreader.int32, when it should be reading an unsigned int. I'm pretty suretableIDshould also be anuint32.read dataTypeID and tableID as unsigned uint
read dataTypeID and tableID as unsigned uint (#3347)
pyrocks commentedon Mar 20, 2025
Hi, seeing
sequelize/packages/postgres/package.json
Line 44 in 73edf49
when can we expect it?
thanks
WikiRik commentedon Mar 20, 2025
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