Skip to content

FEATURE: Support ENUM naming for ENUM reuse. #2577

Description

@tiblu

Intro
I want to name my ENUM type so that I could use the same ENUM in several Models.

The need
Right now ENUM name is created enum_ + ModelNamePlural + _ + fieldName. Problem arises when I "extend" a Model and inherit ENUM field 2 differently named ENUMs are created in the DB thus I have to do casting to actually compare the 2 in the queries.

Proposal
ENUM type to take in "name" as parameter. That is:

            level: {
                type: DataTypes.ENUM('myEnumName'),
                values: ['admin', 'edit', 'read', 'none'],
                comment: 'User membership level.'
            }

The generated name would be enum_ + MyEnumName.

Activity

  1. added
    type: feature
    on Dec 3, 2014
  2. frogcjn commented on Jun 16, 2016

    @frogcjn
    Contributor

    Any progress for this feature?

  3. michaelsemyonov commented on Jul 15, 2016

    @michaelsemyonov

    Hi, any news on this?

  4. apsavin commented on Jul 17, 2016

    @apsavin

    I think, it's better to use the following syntax:

    Sequelize.ENUM({
        name: 'myEnumName',
        values: ['one', 'two']
    })

    Because ENUM already can accepts values as shown above.
    But here is a problem: nobody will want to write values every time when ENUM reuse needed.
    So, may be, it's better to come with something like custom type definition syntax:

    sequelize.defineType('myEnumName', {
      as: Sequelize.ENUM, // or Sequelize.RANGE, or nothing, if you don't need `AS` in your query
      values: [...], // optional, for ENUM
      ...
    })
  5. mickhansen commented on Jul 17, 2016

    @mickhansen
    Contributor

    Might make more sense to simply allow reusing of the ENUM type (+ naming)

  6. apsavin commented on Jul 17, 2016

    @apsavin

    It seems that it's not too hard to add naming to the ENUM type (for postgres at least).

  7. michaelsemyonov commented on Jul 18, 2016

    @michaelsemyonov

    @apsavin @mickhansen Thanks, guys.

  8. felixfbecker commented on Jul 18, 2016

    @felixfbecker
    Contributor

    +1 for this syntax

    const myEnum = Sequelize.ENUM({
        name: 'myEnumName',
        values: ['one', 'two']
    })
    
    sequelize.define('User', {something: myEnum})
  9. sturdynut commented on Apr 10, 2019

    @sturdynut

    Any traction on this? Do you want help w/implementing this?

    Currently in a situation where we could really use this feature.

  10. tiblu commented on Apr 10, 2019

    @tiblu
    ContributorAuthor

    I guess we need someone to PR this. It should not be that hard and Sequelize has very comprehensive automated tests so it's easy to test and verify that nothing else was broken.

  11. stale commented on Jul 23, 2019

    @stale
  12. 3 remaining items

  13. added
    status: understood
    on Jul 31, 2019
  14. sturdynut commented on Sep 3, 2019

    @sturdynut

    @papb - I will do my best to do up a PR soon.

  15. sturdynut commented on Sep 20, 2019

    @sturdynut

    Just a follow up. I am making progress on this, slowly but surely.

  16. papb commented on Oct 1, 2019

    @papb
    Member

    @sturdynut Awesome, thanks! Take your time :)

  17. added
    status: wip
    dialect: postgres
    and removed
    status: understood
    on Oct 1, 2019
  18. Betree commented on Apr 30, 2021

    @Betree

    I'm currently using the following workaround to share an enum between two models, it seems to work:

    Migration

    await queryInterface.createTable('TransactionSettlements', {
    	kind: {
    		// Re-using the same ENUM than `Transactions` so that we don't have to maintain two of them
    		type: '"public"."enum_Transactions_kind"',
    		allowNull: false,
    	},
    	...
    });

    Model

    class TransactionSettlement extends Model {
    	kind: TransactionKind; // Typescript enum from `Transactions`
    	...
    }
    
    TransactionSettlement.init(
    	kind: {
      		// Re-using the same ENUM than `Transactions` so that we don't have to maintain two of them.
     	 	// We'll have a better way of doing this with https://github.com/sequelize/sequelize/issues/2577
      		type: '"public"."enum_Transactions_kind"',
      		allowNull: false,
    	},
    	...
    }
  19. jthoward64 commented on Apr 26, 2023

    @jthoward64

    Just from a cursory look (correct me if I'm wrong) this looks like as of V7 all that is needed is changing the pgEnum* functions, the ENUM toSql function and the constructor/type (plus tests). Anybody who understand Sequelize's ENUM implementation more than I do know if there would be anything else? If that's all it is I might be able to find some time and PR that.

  20. jthoward64 commented on Apr 26, 2023

    @jthoward64

    Also the behavior/syntax I'm thinking (to be friendly with typescript and decorators) is something like the following (this is from a failed attempt at using custom types for this behavior)

    export class NamedEnum<
      Member extends string = string
    > extends DataTypes.ENUM<Member> {
      name: string;
    
      constructor(options: EnumOptions<Member> & { name: string }) {
        super(options);
        this.name = options.name;
      }
    
      toSql() {
        const context = this.usageContext;
        if (context == null) throw new Error("No usage context");
    
        return context.sequelize.queryGenerator.quoteIdentifier(this.name);
      }
    }
    
    export const AnEnum: NamedEnum = new NamedEnum({
      name: "enum_an_enum",
      values: ["some", "values"],
    });

    Then you would simply pass AnEnum in for the data type of an attribute

  21. ephys commented on Apr 26, 2023

    @ephys
    Member

    That's correct, we redesigned the enum type in V7 to make it easier to implement this feature. Combined with a recent change to add "if not exists" to the enum creation query, it should be possible to implement this with few changes

  22. RWOverdijk commented on Jan 12, 2024

    @RWOverdijk

    Any in-between solution for us v6 users?

  23. added theissue type on Mar 9, 2025
  24. removed
    type: feature
    on Mar 9, 2025
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: postgresstatus: wip

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions