简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM