4

I have a question;

Has anyone experienced to migrate a table using CakePHP 3 Migration Tool that when a specific field is an ENUM data type, the migration script automatically converts it into string or text.

How can I avoid it and how can I maintain the data type from ENUM?

Thanks

bowmeow
  • 109
  • 1
  • 11
  • Same problem with CakePHP 4 and MariaDB. Changing generated migration as recommended by @code-kobold worked. Applied migration on production system and database was correctly updated, enum column added and default value set for existing records. – Yevgeniy May 14 '22 at 06:38

1 Answers1

5

It depends on the driver in use, since enum is not supported by all database systems. For the MySQL driver, using the enum type will result in the appropriate DDL.

Migration class:

public function up()
{
    $table = $this->table('testenum');
    $table
        ->addColumn('enum_column', 'enum', [
            'values' => ['one', 'two']
        ])
        ->create();
}

DDL:

CREATE TABLE `testenum` (
  `enum_column` enum('one','two') NOT NULL,
  PRIMARY KEY (`id`)
) 

In the Phinx package, enum is present only in the MysqlAdapter.

Community
  • 1
  • 1
code-kobold
  • 829
  • 14
  • 18