使用 UUID 数组列类型的 psycopy2 错误
Error from psycopy2 with UUID-array column type
我无法将 UUID 值插入 Postgres 中的 UUID 数组列。
由于 psycopg2 不会自动将 Python UUID 值转换为 Postgres UUID,我使用类似 str(my_id)
然而,这是我第一次使用任何数组类型的列。我得到的错误是:
psycopg2.ProgrammingError: column "items" is of type uuid[] but expression is of type text[]
LINE 3: ...0', NULL, '01adae1e-e2cf-11e4-b773-ac8112c8e1fb', ARRAY['01a...
^
HINT: You will need to rewrite or cast the expression.
我用谷歌搜索了这个错误并发现了一些(例如 bug 2279, bug 2277)Postgres 错误报告暗示 incomplete/missing 数组功能的实现,但我不知道这些是否与我的问题有关。然而,这些似乎已经得到解决。 FWIW 我正在使用 Postgres 9.3.6
为了调试这个,我创建了一个小测试 table,例如:
testdb=# \d test_table
Table "public.test_table"
Column | Type | Modifiers
-----------+---------+-----------
key | integer |
item_list | uuid[] |
以及我的健谈测试的输出:
DEBUGGING:
Statement: INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);
Arguments: [['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2f28dd7e-e34b-11e4-9b5e-2c768ada7efb'], 1]
Active Role: test1
Mogrified SQL Expression:
INSERT INTO test_table
("item_list", "key")
VALUES (ARRAY['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2f28dd7e-e34b-11e4-9b5e-2c768ada7efb'], 1);
Exception ProgrammingError occured. Arguments:
('column "item_list" is of type uuid[] but expression is of type text[]\nLINE 3: VALUES (ARRAY[\'2f286cea-e34b-11e4-9b5e-2c768ada7efb\', \'2...\n ^\nHINT: You will need to rewrite or cast the expression.\n',)
* * * * WARNING: Command Failure on Running Statement
INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);!
ERROR: column "item_list" is of type uuid[] but expression is of type text[]
LINE 3: VALUES (ARRAY['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2...
^
HINT: You will need to rewrite or cast the expression.
None
Traceback (most recent call last):
File "db-test.py", line 214, in <module>
db_exec(cursor, stmt, args, debug=True, re_raise=True)
File "db-test.py", line 62, in db_exec
cur.execute(stmt, args)
psycopg2.ProgrammingError: column "item_list" is of type uuid[] but expression is of type text[]
LINE 3: VALUES (ARRAY['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2...
^
HINT: You will need to rewrite or cast the expression.
我唯一尝试过的另一件事是使用 uuid raw 但我得到了预期的 "Can't adapt" 错误。
在我的测试程序中,异常确实发生在前一行,例如
DEBUGGING:
Statement: INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);
Arguments: [[UUID('2bfaf276-e34c-11e4-8af5-2c768ada7efb'), UUID('2bfb6d82-e34c-11e4-8af5-2c768ada7efb')], 1]
Active Role: test1
Mogrified SQL Expression:
Exception ProgrammingError occured. Arguments:
("can't adapt type 'UUID'",)
* * * * WARNING: Command Failure on Running Statement
INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);!
None
None
Traceback (most recent call last):
File "db-test.py", line 214, in <module>
db_exec(cursor, stmt, args, debug=True, re_raise=True)
File "db-test.py", line 61, in db_exec
print cur.mogrify(stmt, args)
psycopg2.ProgrammingError: can't adapt type 'UUID'
测试程序的相关部分如下所示:
46 def db_exec(cur, stmt, args=None, re_raise=True, debug=False, assume_role=None):
47 if (cur is None) or (cur is False):
48 print "ERROR : DB Connection/Cursor is not open for commands"
49 return cur
50 if (debug is True):
51 print "DEBUGGING:"
52 print "Statement: ", stmt
53 if args is not None:
54 print "Arguments: ", args
55 try:
56 if assume_role is not None:
57 cur.execute('SET ROLE "%s";' % assume_role)
58 if debug is True:
59 print "Active Role:", GetCurrentRole(cur)
60 print "Mogrified SQL Expression:"
61 print cur.mogrify(stmt, args)
62 cur.execute(stmt, args)
[...snip...]
我看到有一种方法可以为 Python <--> Postgres 添加自定义 "autmatic" 类型转换,我想知道解决方案是否就在那里……但我不知道不知道该怎么做(还)
我决定研究 psycopg2 附加功能,并找到了一个只涉及一行额外代码的解决方案。
提示来自 here,可在该页面底部附近标题 "UUID data type"
下的部分中找到
行是:
psycopg2.extras.register_uuid()
尽管我对简单和数组 UUID 类型的这项工作持悲观态度,但它确实很好。此外,这意味着我不再需要手动将每个 UUID 转换为 str。 (反过来在我的应用程序中不是问题,但大概它也适用于检索 UUID 并将它们注册为 python 中的 UUID 类型)
我无法将 UUID 值插入 Postgres 中的 UUID 数组列。
由于 psycopg2 不会自动将 Python UUID 值转换为 Postgres UUID,我使用类似 str(my_id)
然而,这是我第一次使用任何数组类型的列。我得到的错误是:
psycopg2.ProgrammingError: column "items" is of type uuid[] but expression is of type text[]
LINE 3: ...0', NULL, '01adae1e-e2cf-11e4-b773-ac8112c8e1fb', ARRAY['01a...
^
HINT: You will need to rewrite or cast the expression.
我用谷歌搜索了这个错误并发现了一些(例如 bug 2279, bug 2277)Postgres 错误报告暗示 incomplete/missing 数组功能的实现,但我不知道这些是否与我的问题有关。然而,这些似乎已经得到解决。 FWIW 我正在使用 Postgres 9.3.6
为了调试这个,我创建了一个小测试 table,例如:
testdb=# \d test_table
Table "public.test_table"
Column | Type | Modifiers
-----------+---------+-----------
key | integer |
item_list | uuid[] |
以及我的健谈测试的输出:
DEBUGGING:
Statement: INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);
Arguments: [['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2f28dd7e-e34b-11e4-9b5e-2c768ada7efb'], 1]
Active Role: test1
Mogrified SQL Expression:
INSERT INTO test_table
("item_list", "key")
VALUES (ARRAY['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2f28dd7e-e34b-11e4-9b5e-2c768ada7efb'], 1);
Exception ProgrammingError occured. Arguments:
('column "item_list" is of type uuid[] but expression is of type text[]\nLINE 3: VALUES (ARRAY[\'2f286cea-e34b-11e4-9b5e-2c768ada7efb\', \'2...\n ^\nHINT: You will need to rewrite or cast the expression.\n',)
* * * * WARNING: Command Failure on Running Statement
INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);!
ERROR: column "item_list" is of type uuid[] but expression is of type text[]
LINE 3: VALUES (ARRAY['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2...
^
HINT: You will need to rewrite or cast the expression.
None
Traceback (most recent call last):
File "db-test.py", line 214, in <module>
db_exec(cursor, stmt, args, debug=True, re_raise=True)
File "db-test.py", line 62, in db_exec
cur.execute(stmt, args)
psycopg2.ProgrammingError: column "item_list" is of type uuid[] but expression is of type text[]
LINE 3: VALUES (ARRAY['2f286cea-e34b-11e4-9b5e-2c768ada7efb', '2...
^
HINT: You will need to rewrite or cast the expression.
我唯一尝试过的另一件事是使用 uuid raw 但我得到了预期的 "Can't adapt" 错误。
在我的测试程序中,异常确实发生在前一行,例如
DEBUGGING:
Statement: INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);
Arguments: [[UUID('2bfaf276-e34c-11e4-8af5-2c768ada7efb'), UUID('2bfb6d82-e34c-11e4-8af5-2c768ada7efb')], 1]
Active Role: test1
Mogrified SQL Expression:
Exception ProgrammingError occured. Arguments:
("can't adapt type 'UUID'",)
* * * * WARNING: Command Failure on Running Statement
INSERT INTO test_table
("item_list", "key")
VALUES (%s, %s);!
None
None
Traceback (most recent call last):
File "db-test.py", line 214, in <module>
db_exec(cursor, stmt, args, debug=True, re_raise=True)
File "db-test.py", line 61, in db_exec
print cur.mogrify(stmt, args)
psycopg2.ProgrammingError: can't adapt type 'UUID'
测试程序的相关部分如下所示:
46 def db_exec(cur, stmt, args=None, re_raise=True, debug=False, assume_role=None):
47 if (cur is None) or (cur is False):
48 print "ERROR : DB Connection/Cursor is not open for commands"
49 return cur
50 if (debug is True):
51 print "DEBUGGING:"
52 print "Statement: ", stmt
53 if args is not None:
54 print "Arguments: ", args
55 try:
56 if assume_role is not None:
57 cur.execute('SET ROLE "%s";' % assume_role)
58 if debug is True:
59 print "Active Role:", GetCurrentRole(cur)
60 print "Mogrified SQL Expression:"
61 print cur.mogrify(stmt, args)
62 cur.execute(stmt, args)
[...snip...]
我看到有一种方法可以为 Python <--> Postgres 添加自定义 "autmatic" 类型转换,我想知道解决方案是否就在那里……但我不知道不知道该怎么做(还)
我决定研究 psycopg2 附加功能,并找到了一个只涉及一行额外代码的解决方案。
提示来自 here,可在该页面底部附近标题 "UUID data type"
下的部分中找到行是:
psycopg2.extras.register_uuid()
尽管我对简单和数组 UUID 类型的这项工作持悲观态度,但它确实很好。此外,这意味着我不再需要手动将每个 UUID 转换为 str。 (反过来在我的应用程序中不是问题,但大概它也适用于检索 UUID 并将它们注册为 python 中的 UUID 类型)