8

I want to create a field in my custom table "test_days" with values "days,week,month" and set default value as "days".

How is this possible with Magento 2?

2 Answers 2

16

It's not supported in Magento 2. From the beginning of appearing ENUM data type in MySQL it was considered as a bad practice to use ENUM, because of many reasons. One of main problems with ENUM:

  • ENUM violates rules of data normalization
  • Changing the list of possible ENUM values is very expensive, because it leads to table rebuild with locks during this process
  • ENUM is quite limited, you can’t add additional attributes for your ENUM entities
  • Return list of possible distinct values is also a problem if you don’t store them in a source code
  • ENUM entity couldn’t be re-used in another table if needed. So, developer will need to duplicate ENUM declaration. Which is incorrect as any other code duplication

This short article describes most of drawbacks of ENUM type in MySQL http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/

The preferable option is to use reference table into which an enumerated set of possible values of a certain field data type is divested. Like described here - http://komlenic.com/post_files/image/244/country_tables.png

1
  • I understand these points, but what is truely stupid is that setup:upgrade breaks if the database contains another non-Magento table that does use an enum column 🤯Some people also have a phpMyAdmin installation in the same DB, and run into this. Why does Magento inspect tables it doesn't use?
    – Wouter
    Apr 29, 2020 at 18:12
0

It is NOT violating any rule of data normalization since it is a constraint and you do not need to repeat data in other columns or tables.

Enum is just a data type, not an entity, not data. Enum is only a set of accepted values, is a constraint.

Is not expensive to alter this table column; if you change data is the same expensive as for other data types as well.

You can implement the form field implementing a model like any other cases in magento.

You can re-use enum type in any other table. Referring to the values in discriminatory columns data is not the same thing with de-normalization.

Magento did not implement (yet) this type because it is confusing for many developers. It is not the only type not implemented yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.