Skip to content

Enums data types does not honor schema names in postgres #13804

@nahog

Description

@nahog

Issue Creation Checklist

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).releasedtype: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions