use*_*560 5 python postgresql sqlalchemy
我正在使用多维数组,并且我注意到postgres需要强制转换表达式以插入值,例如:
CREATE TABLE test (
pay integer[]
);
CREATE TABLE
INSERT INTO test values (ARRAY[NULL]);
ERROR: column "pay" is of type integer[] but expression is of type text[]
LINE 1: INSERT INTO test values (ARRAY[NULL]);
^
HINT: You will need to rewrite or cast the expression.
INSERT INTO test values (ARRAY[NULL::integer]); -- How to do this on SqlAlchemy ?
INSERT 0 1 -- ARRAY[NULL]::integer[] would also works
Run Code Online (Sandbox Code Playgroud)
这就是我添加对象时SqlAlchemy所做的事情,如果VALUE为NULL,则不会进行类型转换。这是我的代码的一部分:
from sqlalchemy.dialects.postgresql import ARRAY
class Test (Base):
__tablename__ = 'test'
pay = Column(ARRAY(Integer))
member = Test()
member.pay = [None]
session.add(member)
session.commit()
Run Code Online (Sandbox Code Playgroud)
然后在postgres日志中,我得到:
ERROR: column "pay" is of type integer[] but expression is of type text[] at character 26
HINT: You will need to rewrite or cast the expression.
STATEMENT: INSERT INTO test (pay) VALUES (ARRAY[NULL]); -- See ?? No casting from SqlAlchemy
Run Code Online (Sandbox Code Playgroud)
所以问题是:当列表中的值为None时,我还能为SqlAlchemy进行类型转换吗?也许一些自定义类型的实现?
| 归档时间: |
|
| 查看次数: |
2163 次 |
| 最近记录: |