db-migration icon indicating copy to clipboard operation
db-migration copied to clipboard

Migrations: Support ENUM field

Open saada opened this issue 10 years ago • 29 comments

ENUM("ONE", "TWO", "THREE") NOT NULL'

should be supported via

$this->enum(['ONE', 'TWO', 'THREE'])->notNull();

saada avatar Sep 29 '15 21:09 saada

+1

usualdesigner avatar Sep 30 '15 19:09 usualdesigner

+1

wbraganca avatar Oct 02 '15 17:10 wbraganca

Is enum supported by all DBs that are supported in Yii?

samdark avatar Oct 02 '15 17:10 samdark

@samdark even if not directly supported by a db, adding a check constraint to limit values makes it look like an enum:

ALTER TABLE table ADD CONSTRAINT enum_col CHECK (enum_col IN ('value1', 'value2'));

CHECK constraints should be supported by most dbs, even SQLite.

nineinchnick avatar Oct 04 '15 09:10 nineinchnick

The drawback is that such a CHECK constraint won't be detected as an enum when Yii loads the schema and populates \yii\db\Column::$enumValues.

nineinchnick avatar Oct 04 '15 10:10 nineinchnick

So Gii won't be able to generate based on such checks. And since we don't have it in schema we won't be able to work with these in a good way...

samdark avatar Oct 04 '15 10:10 samdark

We could have only one supported syntax and detect it, like the example i've posted above. I've already done detecting literal values when parsing column defaults in #9773.

nineinchnick avatar Oct 04 '15 10:10 nineinchnick

I actually use such CHECK syntax often and would be interested if it gets detected, because now I have to add it in the model manually.

nineinchnick avatar Oct 04 '15 10:10 nineinchnick

It would be great to have support for an API like

$this->addEnumValue($tableName, $columnName, ['NEW_ENUM_VALUE']);
$this->removeEnumValue($tableName, $columnName, ['NEW_ENUM_VALUE']);

saada avatar Mar 22 '16 20:03 saada

+1

gitowiec avatar May 02 '16 11:05 gitowiec

The biggest challenge with adding/removing ENUM values from a column is migrating old records to the new values. Maybe that should be the responsibility of the developer to write the proper updateAll() statements in their migration?

saada avatar May 02 '16 15:05 saada

I assume that adding a default value can solve the problem with the data migration. Another way is to migrate by adding a new values, doing some changes after that, and finally removing unnecessary values from enum.

arren-ru avatar May 10 '16 05:05 arren-ru

+1

kiironoaki avatar Nov 09 '16 13:11 kiironoaki

+1

dignityinside avatar Dec 18 '16 15:12 dignityinside

+1

tommyknocker avatar Jan 13 '17 13:01 tommyknocker

We are glad to accept a pull request with the suggested enhancement

SilverFire avatar Jan 14 '17 14:01 SilverFire

+1

newerton avatar Jun 20 '17 22:06 newerton

+1

planet17 avatar Aug 08 '17 07:08 planet17

+1

paulocoutinhox avatar Aug 16 '17 06:08 paulocoutinhox

how to add default value in enum ??

harsiddhi avatar Aug 22 '17 18:08 harsiddhi

ENUM('a', 'b', 'c') NOT NULL DEFAULT 'a'?

cebe avatar Aug 22 '17 18:08 cebe

+1

designia avatar Aug 26 '17 15:08 designia

Is anyone working on this?

santilin avatar Oct 05 '17 11:10 santilin

No.

samdark avatar Oct 05 '17 12:10 samdark

@samdark is this done?

NetLS2013 avatar Jan 02 '19 09:01 NetLS2013

No.

samdark avatar Jan 02 '19 23:01 samdark

I really want to have enum and set column type support :-)

tomaszkane avatar Feb 25 '21 16:02 tomaszkane

Is this feature still in progress?

Radon8472 avatar Oct 14 '22 07:10 Radon8472

It is on hold.

samdark avatar Oct 14 '22 12:10 samdark