简体   繁体   English

Mysql将空ENUM转换为Postgresql ENUM

[英]Mysql empty ENUM to postgresql ENUM

I want to convert a MySQL database to PostgreSQL. 我想将MySQL数据库转换为PostgreSQL。

In MySQL, ENUM data type allows by default empty fields, which seems not to work with PostgreSQL. 在MySQL中,默认情况下ENUM数据类型允许使用空字段,这似乎不适用于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. 因此,我无法使用PostgreSQL从VARCHAR转换为ENUM,因为我的数据库中有很多PgSQL不允许的空字段。

What may I do ? 我该怎么办? Allow NULL and set empty fields to NULL ? 允许NULL并将空字段设置为NULL吗? Add an empty value in PostgreSQL's ENUM when creating the type (something like ENUM('A','B','C','')) ? 创建类型时(在ENUM('A','B','C','')之类的类型中,在PostgreSQL的ENUM中添加一个空值)? Stop using this ugly and incongruous ENUM data type (and use a constraint on varchar instead or another table and a foreign key) ? 停止使用这种丑陋且不一致的ENUM数据类型(并改用varchar约束或其他表和外键)?

Thank you :) 谢谢 :)

user1527491 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. 如果想法是“未知”,那么使用NULL也是一个不错的选择-从纯模型的角度来看,可能更简洁。

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. 归结为一个经典的问题,当某些数据库和系统的值明显不同时,它们会将null和空字符串视为同一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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