如何确定"value too long for type character varying"涉及到哪一列?
How to determine which column is implicated in "value too long for type character varying"?
我正在使用 Python 和 psycopg 以编程方式将数据添加到 PostgreSQL table - 这工作正常。
但有时文本值对于包含的列而言太长,所以我收到消息:
ERROR: value too long for type character varying(1000)
其中数字是违规列的宽度。
有没有办法确定哪一列导致了错误? (除了比较每一列的长度看是否为1000)
我认为没有简单的方法。
我试图在 psql 中设置 VERBOSITY,因为我认为这会有所帮助,但不幸的是没有(在 9.4 上):
psql
\set VERBOSITY verbose
dbname=> create temporary table test (t varchar(5));
CREATE TABLE
dbname=> insert into test values ('123456');
ERROR: 22001: value too long for type character varying(5)
LOCATION: varchar, varchar.c:623
这可能值得在邮件列表中进行讨论,如 you are not the only one with this problem。
非常感谢@Tometzky,他的评论为我指明了正确的方向。
在事实发生后,我没有试图确定是哪个列导致了问题,而是修改了我的Python脚本以确保该值被截断在 插入数据库之前。
使用select column_name, data_type, character_maximum_length from information_schema.columns where table_name='test'
访问table的架构
构建 INSERT 语句时,使用模式定义来标识字符字段并在必要时截断
我正在使用 Python 和 psycopg 以编程方式将数据添加到 PostgreSQL table - 这工作正常。
但有时文本值对于包含的列而言太长,所以我收到消息:
ERROR: value too long for type character varying(1000)
其中数字是违规列的宽度。
有没有办法确定哪一列导致了错误? (除了比较每一列的长度看是否为1000)
我认为没有简单的方法。
我试图在 psql 中设置 VERBOSITY,因为我认为这会有所帮助,但不幸的是没有(在 9.4 上):
psql
\set VERBOSITY verbose
dbname=> create temporary table test (t varchar(5));
CREATE TABLE
dbname=> insert into test values ('123456');
ERROR: 22001: value too long for type character varying(5)
LOCATION: varchar, varchar.c:623
这可能值得在邮件列表中进行讨论,如 you are not the only one with this problem。
非常感谢@Tometzky,他的评论为我指明了正确的方向。
在事实发生后,我没有试图确定是哪个列导致了问题,而是修改了我的Python脚本以确保该值被截断在 插入数据库之前。
使用
select column_name, data_type, character_maximum_length from information_schema.columns where table_name='test'
访问table的架构
构建 INSERT 语句时,使用模式定义来标识字符字段并在必要时截断