user1527491
user1527491

Reputation: 965

Mysql empty ENUM to postgresql ENUM

I want to convert a MySQL database to PostgreSQL.

In MySQL, ENUM data type allows by default empty fields, which seems not to work with PostgreSQL. Therefore, I can't cast from VARCHAR to ENUM with PostgreSQL because I have a lot of empty fields in my database that PgSQL does not allow.

What may I do ? Allow NULL and set empty fields to NULL ? Add an empty value in PostgreSQL's ENUM when creating the type (something like ENUM('A','B','C','')) ? Stop using this ugly and incongruous ENUM data type (and use a constraint on varchar instead or another table and a foreign key) ?

Thank you :)

user1527491

Upvotes: 2

Views: 2636

Answers (1)

Magnus Hagander
Magnus Hagander

Reputation: 25158

If you actually want to have empty string values in the enum, you should add them to the enum when creating the type, yes. Any database that accepts the empty string into an enum that does not explicitly allow an empty string is buggy, and if your application relies on it, that's buggy too :)

If the idea is that it means "unknown", then using NULL is also a good choice - from a pure model perspective, probably the cleaner one.

It comes down to the classic problem that some databases and systems consider null and the empty string to be the same thing, when they are clearly distinct values.

Upvotes: 5

Related Questions