You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FEATURE: Support ENUM naming for ENUM reuse. #2577
dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).status: wipFor issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started.
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:
Reacted by Pavel K., Pedro de Assis, George Bell, Pedro Augusto de Paula Barbosa, WooYoung Cho, Nate, Nico Chaves, Eugene Shemonaev, Benjamin Piouffle, Edmundo Santos and 9 more
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 queryvalues: [...],// optional, for ENUM
...
})
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.
Reacted by Matti Salokangas, Roman Danylyshyn, Antony and Tag Howard
I'm currently using the following workaround to share an enum between two models, it seems to work:
Migration
awaitqueryInterface.createTable('TransactionSettlements',{kind: {// Re-using the same ENUM than `Transactions` so that we don't have to maintain two of themtype: '"public"."enum_Transactions_kind"',allowNull: false,},
...
});
Model
classTransactionSettlementextendsModel{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/2577type: '"public"."enum_Transactions_kind"',allowNull: false,},
...
}
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.
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)
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
dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).status: wipFor issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started.
Activity
frogcjn commentedon Jun 16, 2016
Any progress for this feature?
michaelsemyonov commentedon Jul 15, 2016
Hi, any news on this?
apsavin commentedon Jul 17, 2016
I think, it's better to use the following syntax:
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:
mickhansen commentedon Jul 17, 2016
Might make more sense to simply allow reusing of the ENUM type (+ naming)
apsavin commentedon Jul 17, 2016
It seems that it's not too hard to add naming to the ENUM type (for postgres at least).
michaelsemyonov commentedon Jul 18, 2016
@apsavin @mickhansen Thanks, guys.
felixfbecker commentedon Jul 18, 2016
+1 for this syntax
sturdynut commentedon Apr 10, 2019
Any traction on this? Do you want help w/implementing this?
Currently in a situation where we could really use this feature.
tiblu commentedon Apr 10, 2019
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.
stale commentedon Jul 23, 2019
3 remaining items
sturdynut commentedon Sep 3, 2019
@papb - I will do my best to do up a PR soon.
sturdynut commentedon Sep 20, 2019
Just a follow up. I am making progress on this, slowly but surely.
papb commentedon Oct 1, 2019
@sturdynut Awesome, thanks! Take your time :)
Betree commentedon Apr 30, 2021
I'm currently using the following workaround to share an enum between two models, it seems to work:
Migration
Model
- Switched to countries as integer (Due to sequelize/sequelize#2577) …
jthoward64 commentedon Apr 26, 2023
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.
jthoward64 commentedon Apr 26, 2023
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)
Then you would simply pass
AnEnumin for the data type of an attributeephys commentedon Apr 26, 2023
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
RWOverdijk commentedon Jan 12, 2024
Any in-between solution for us v6 users?