小编xmo*_*xmo的帖子

禁用带引号的值到整数的隐式转换

# CREATE TABLE foo ( id serial, val integer );
CREATE TABLE
# INSERT INTO foo (val) VALUES ('1'), (2);
INSERT 0 2
# SELECT * FROM foo WHERE id='1';
 id | val 
----+-----
  1 |   1
(1 row)
Run Code Online (Sandbox Code Playgroud)

这里,在插入和选择时,postgres 都会隐式地将带引号的字符串转换为整型,而不是引发类型错误,除非带引号的值非常明确地键入为 varchar:

# INSERT INTO foo (val) VALUES (varchar '1');
ERROR:  column "val" is of type integer 
        but expression is of type character varying
LINE 1: INSERT INTO foo (val) VALUES (varchar '1');
                                              ^
HINT:  You will need to rewrite …
Run Code Online (Sandbox Code Playgroud)

postgresql

2
推荐指数
1
解决办法
3199
查看次数

标签 统计

postgresql ×1