简体   繁体   English

未设置枚举值,但不为空

[英]Enum value is not set, BUT is not null

I have a row in a table that is an enum type, but can also be NULL by default. 我的表中有一行是枚举类型,但默认情况下也可以为NULL。
Empty string ( '' ) is not one of the possible enum value, and yet, after some time, I found out that all of the entries I thought to be null were actually set to an empty string. 空字符串( ''不是可能的枚举值之一,但是一段时间后,我发现所有我认为为空的条目实际上都设置为空字符串。

Fixing this wasn't a problem. 解决这个问题不是问题。 But, I'm willing to know how this could even happen in the first place, -to make sure I don't get any of these ever again,- but so far I haven't been able to recreate new entries with an empty string as value. 但是,我很想知道这种情况一开始是怎么发生的,以确保我再也不会遇到这些了,但是到目前为止,我还不能用空的方式重新创建新条目。字符串作为值。

What could I have that would cause an enum value to be set neither to null nor any of the possible values? 我可能会导致枚举值既不设置为null也不设置任何可能的值?

If someone tries to insert a value to the enum that is not in its defined list of values, and strict mode is not enforced, then the value will be truncated to '' . 如果有人尝试将一个不在其定义的值列表中的值插入到枚举中,并且未强制执行严格模式,则该值将被截断为''

mysql> create table t (e enum('a','b','c'));

mysql> insert into t set e='d';
ERROR 1265 (01000): Data truncated for column 'e' at row 1

mysql> set session sql_mode=''; -- disable strict mode

mysql> insert into t set e='d';
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'e' at row 1 |
+---------+------+----------------------------------------+

mysql> select * from t;
+------+
| e    |
+------+
|      |
+------+

https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html says: https://dev.mysql.com/doc/refman/5.7/zh-CN/sql-mode.html说:

Strict mode produces an error for attempts to create a key that exceeds the maximum key length. 对于试图创建超过最大密钥长度的密钥,严格模式会产生错误。 When strict mode is not enabled, this results in a warning and truncation of the key to the maximum key length. 如果未启用严格模式,则会导致警告并把密钥截断为最大密钥长度。

It doesn't say so explicitly, but "exceeds the maximum length" also includes "not an element of the enum." 它没有这么明确地说,但是“超过最大长度”还包括“不是枚举的元素”。

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

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