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?


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

  • 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 '20 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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