Skip to content

Migration on ENUM with postgres dialect: type already exists #2554

Closed

Description

Deleting previously existing ENUM column and re-creating it but with a different ENUM results in:

Executing (default): ALTER TABLE "Trackers" DROP COLUMN "type";
Executing (default): CREATE TYPE "enum_Trackers_type" AS ENUM('image', 'javascript', 'snippet'); ALTER TABLE "Trackers" ADD COLUMN "type" "enum_Trackers_type";
Possibly unhandled SequelizeDatabaseError: error: type "enum_Trackers_type" already exists

Activity

  1. janmeier commented on Nov 10, 2014

    @janmeier
    Member

    That's a postgres issue. Dropping an enum column does not drop the enum itself (since an enum might be used in several tables as I understand it)

  2. added
    type: feature
    type: bug
    on Dec 4, 2014
  3. laser commented on Jan 9, 2015

    @laser

    @kytwb

    Any idea how to correctly edit enum values through migration then?

    I was able to modify an ENUM by:

    1. Modifying the array assigned to the values property of the configuration object passed as the second argument to sequelize.define to include a new value
    2. Create a migration in which the up simply calls ModelNameHere.sync()

    That generated the SQL statements required to modify the ENUM-type to match what I'd expressed in JavaScript code.

    Erin

  4. Victor08 commented on Feb 6, 2016

    @Victor08

    I am facing the same issue. I was able to bypass it with using raw query

    module.exports = {
      up: function (queryInterface, Sequelize) {
        return queryInterface.sequelize.query("CREATE TYPE \"enum_users_sellerAccountType\" AS ENUM(\'sa\', \'sm\'); ALTER TABLE \"users\" ADD COLUMN \"sellerAccountType\" \"enum_users_sellerAccountType\";");
      },
    
      down: function (queryInterface, Sequelize) {
        return queryInterface.sequelize.query("DROP TYPE \"enum_users_sellerAccountType\"; ALTER TABLE \"users\" DROP COLUMN \"sellerAccountType\";");
      }
    };
    

    Is it a bug in sequelize, that causes enum migration to fail?

  5. delveintechnolabs commented on Mar 12, 2016

    @delveintechnolabs

    Facing same issue. To add to the problem, sequelize only allows changing default value, allowance of null or the data type. If you want to change some other attribute of a column, you will have to find a hack for it.

  6. thealmightygrant commented on Oct 4, 2016

    @thealmightygrant

    Heads up that if you decide to do raw queries like @Victor08 points out above, the down migration should drop the column first so that nothing depends on the data type, like this:

     down: function (queryInterface, Sequelize) {
        return queryInterface.sequelize.query("ALTER TABLE \"users\" DROP COLUMN \"userType\"; DROP TYPE \"enum_users_userType\";");
      }
  7. felixfbecker commented on Oct 5, 2016

    @felixfbecker
    Contributor

    We have QueryInterface.dropAllEnums(), I think we should add a simple dropEnum() method.

  8. kgalli commented on Feb 21, 2017

    @kgalli

    @felixfbecker is there an update on the dropEnum() method? This would really be very handy. What is the context of QueryInterface.dropAllEnums()? Is it the whole database or maybe a table?

  9. felixfbecker commented on Feb 21, 2017

    @felixfbecker
    Contributor

    No, there is no update. Can't answer your second question.

  10. stale commented on Jun 29, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  11. 13 remaining items

  12. abelosorio commented on Jun 18, 2018

    @abelosorio

    @naartjie @tomprogers @jraut is not possible to "change" an enum in Postgres. You have to destroy it and create it again with the new values.

    Checkout this issue #7151 and this comment #7151 (comment).

    I created this npm package to change enums in Postgres: https://www.npmjs.com/package/sequelize-replace-enum-postgres. Hope it helps.

    Good luck!

  13. robcresswell commented on Aug 20, 2018

    @robcresswell

    @abelosorio It seems like you've been able to alter ENUMs since at least Postgres 9.1https://www.postgresql.org/docs/9.1/static/sql-altertype.html so I'm not sure why you are saying it isn't possible. Can we get this issue reopened @sushantdhiman ?

  14. rostismal commented on Dec 17, 2018

    @rostismal

    is not fixed

  15. DaddyWarbucks commented on Apr 10, 2019

    @DaddyWarbucks

    This may be a bit off topic to this particular thread, but I wanted to post my solution to a similar problem incase anyone ends up here like I did.

    I was trying to set an ENUM along with allowNull: true, then do a bulk update, and then changeColumn to allowNull: false

    await queryInterface.addColumn(
            'payments',
            'entity_type',
            {
              type: Sequelize.ENUM,
              allowNull: true // set to true to allow bulk update
              values: [SHIPPER, CARRIER]
            },
            { transaction }
          );
    
    await queryInterface.bulkUpdate(
            'payments',
            {
              entity_id: 1,
              entity_type: SHIPPER
            },
            {},
            { transaction }
          );
    
    await queryInterface.changeColumn(
            'payments',
            'entity_type',
            {
              type: Sequelize.ENUM,
              allowNull: false,
              values: [SHIPPER, CARRIER]
            },
            { transaction }
          );
    

    ^^ This code threw the error enum_payments_entity_type already exists. Even when trying to manually drop it, alter the column, etc, I could never get it to work.
    My solution was to set the column as a STRING first, then come back and set it as an ENUM.

    await queryInterface.addColumn(
            'payments',
            'entity_type',
            {
              type: Sequelize.STRING, // NOTE THIS IS A STRING
              allowNull: true // set to true to allow bulk update
            },
            { transaction }
          );
    
    await queryInterface.bulkUpdate(
            'payments',
            {
              entity_id: 1,
              entity_type: SHIPPER
            },
            {},
            { transaction }
          );
    
    await queryInterface.changeColumn(
            'payments',
            'entity_type',
            {
              type: Sequelize.ENUM,
              allowNull: false,
              values: [SHIPPER, CARRIER]
            },
            { transaction }
          );
    

    I am not very well versed in Sequelize (or in RDMS in general) but this bug kicked my butt. Hopefully if this is helpful to someone. And if this is bad practice, feel free to downvote it and tell me why.

  16. Dakkers commented on Jul 16, 2019

    @Dakkers

    still not fixed 😭

  17. Otavioensa commented on Jul 23, 2019

    @Otavioensa

    I'm still running on the same problem

  18. Otavioensa commented on Jul 23, 2019

    @Otavioensa

    This issue should definitely be reopened

  19. papb commented on Jul 23, 2019

    @papb
    Member

    Hello, this thread is very long and was closed twice already... Can someone please provide an up-to-date MVCE for this? Something that I can just copy+paste and run and see the problem. If I can easily reproduce it, I will reopen this issue. Thanks!!

  20. added
    dialect: postgres
    and removed
    type: feature
    on Jul 23, 2019
  21. N1ades commented on Dec 14, 2019

    @N1ades

    i have this problem with code:

    module.exports = (sequelize, DataTypes) => {
    	const tag = sequelize.define('tag', {
    		name: {allowNull: false, type: DataTypes.TEXT},
    		type: {allowNull: false, type: DataTypes.ENUM('lang', 'category', 'author')}
    	}, {});
    	tag.associate = function (models) {
    		tag.belongsToMany(models.chapter, {through: 'titlesTags'});
    
    		tag.belongsToMany(models.user, {through: 'tagBlackList'});
    
    	};
    	return tag;
    };
    

    error:
    (node:24616) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: тип "enum_tags_type" уже существует

  22. papb commented on Jan 16, 2020

    @papb
    Member

    I think issues #2577 and #7649 cover everything mentioned in this issue already.

  23. navsaini53 commented on Mar 3, 2022

    @navsaini53
  24. RasenGUY commented on Sep 11, 2022

    @RasenGUY

    @kytwb

    Any idea how to correctly edit enum values through migration then?

    I was able to modify an ENUM by:

    1. Modifying the array assigned to the `values` property of the configuration object passed as the second argument to `sequelize.define` to include a new value
    
    2. Create a migration in which the `up` simply calls `ModelNameHere.sync()`
    

    That generated the SQL statements required to modify the ENUM-type to match what I'd expressed in JavaScript code.

    Erin

    this works thanks for sharing.

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

    dialect: postgrestype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions