Repository: sequelize/sequelize

Issue #13204: Custom types on arrays in posgres generates the wrong name when using underscores in columns
Created: 2021-04-16T20:05:21Z
Issue Description
When using underscored column names in pg enum array types, bulkUpdate queries fails (Actually this will happen for any field uses the "field" property to rename the column, as the data-type file in the postgres dialect is not using the correct "overwritten" propery)
What are you doing?
Here is the link to the SSCCE for this issue: sequelize/sequelize-sscce#158
What do you expect to happen?
I want that when using a model with underscored columns on the table in a postgres database, custom types in array are casted to the proper type when doing queries (in particular bulkCreate)
What is actually happening?
For a simple model like:
        BookDetails.init({
            uniqueName: {
                type: Sequelize.DataTypes.STRING(100),
                unique: true,
                allowNull: false,
                primaryKey: true
            },
            originalCategories: {
                type: Sequelize.DataTypes.ARRAY(Sequelize.DataTypes.ENUM({
                  values: ['drama', 'comedy'],
                })),
              },
        }, {
            underscored: true,
            modelName: 'BookDetails',
            sequelize
        });
when executing the bulkCreate I'm expecting that the data is inserted, but instead fails
        await BookDetails.bulkCreate([{
            uniqueName: 'test',
            originalCategories: ['drama']
        }]);
the error output is clear
INSERT INTO "book_details" ("unique_name","original_categories","created_at","updated_at") VALUES ('test',ARRAY['drama']::"enum_book_details_originalCategories"[],'2021-04-16 19:38:40.298 +00:00','2021-04-16 19:38:40.298 +00:00') RETURNING "unique_name","original_categories","created_at","updated_at";
enum_book_details_originalCategories is not the proper type in the database, it should be enum_book_details_original_categories respecting the underscoring.
Additional context
Environment

Sequelize version: 6.6.2
Node.js version: v12.22.1
Operating System: Ubuntu 20.04

Issue Template Checklist
How does this problem relate to dialects?


 I think this problem happens regardless of the dialect.
 I think this problem happens only for the following dialect(s): postgres
 I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

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 don't know how to start, I would need guidance.
 No, I don't have the time, although I believe I could do it if I had the time...
 No, I don't have the time and I wouldn't even know how to start.

Dev notes
I locally patched my code with a simple sed command:
sed -i 's/options\.field\.fieldName/options\.field\.field/g' ./node_modules/sequelize/lib/dialects/postgres/data-types.js
And from what I followed in the code is just a change on line 483 at lib/dialects/postgres/data-types.js instead of
Utils.generateEnumName(options.field.Model.getTableName(), options.field.fieldName), the code should read           Utils.generateEnumName(options.field.Model.getTableName(), options.field.field), (I debuged it to confirm and the sed patch works locally).
So the change is realively easy, my only concern is extra testing that may be needed.

Duplicate issue #13804: Enums data types does not honor schema names in postgres
Created: 2021-12-21T16:38:51Z
Issue Creation Checklist

 I have read the contribution guidelines

Bug Description
For postgres array of enum types.
When sequelize generates the enum name for bulk create queries for an ARRAY datat type is not considering that enums can be inside a schema, and tries to use the full name of the object (schema).(table) and adds enum_ at the begining.
This is not the logic used for creating the initial value of the enum and the final enum name is invalid. If there is a schema present it should only add enum_ to the table name and use the schema for reference at the begining of the type.
SSCCE

Here is the link to the SSCCE for this issue: sequelize/sequelize-sscce#204

What do you expect to happen?

I want that when using a model with a property that is an array of enums on the table in a postgres database, custom types in array are casted to the proper type when doing queries (in particular bulkCreate).
What is actually happening?

For a simple model like: (look that the schema is not the default and is redefined to: 'test')
  BookDetails.init({
    uniqueName: {
        type: Sequelize.DataTypes.STRING(100),
        unique: true,
        allowNull: false,
        primaryKey: true
    },
    originalCategories: {
        type: Sequelize.DataTypes.ARRAY(Sequelize.DataTypes.ENUM({
          values: ['drama', 'comedy'],
        })),
      },
  }, {
      underscored: true,
      modelName: 'BookDetails',
      schema: 'test',
      sequelize
  });
when executing the bulkCreate I'm expecting that the data is inserted, but instead fails
  await BookDetails.bulkCreate([{
      uniqueName: 'test',
      originalCategories: ['drama']
  }]);
the error output is clear:
INSERT INTO "test"."book_details" ("unique_name","original_categories") VALUES ('test',ARRAY['drama']::"enum_test.book_details_original_categories"[]) RETURNING "unique_name","original_categories";`,
    parameters: undefined
  },
  original: error: type "enum_test.book_details_original_categories[]" does not exist
enum_test.book_details_original_categories is not the proper type in the database, it should be test.enum_book_details_original_categories respecting the underscoring.
enum_test.book_details_original_categories is not the proper type in the database, it should be test.enum_book_details_original_categories respecting the schema name.
Additional context
Environment

Sequelize version: 6.12.1 
Node.js version: 14.18.2 
Operating System: Ubuntu 20.04

Bug Report Checklist

How does this problem relate to dialects?


 I think this problem happens regardless of the dialect.
 I think this problem happens only for the following dialect(s): postgres
 I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

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 don't know how to start, I would need guidance.
 No, I don't have the time, although I believe I could do it if I had the time...
 No, I don't have the time and I wouldn't even know how to start.

Closing comment (2021-12-21T16:39:55Z): This issue is similar to #13204