我正在使用sqlalchemy 1.2
简而言之,产生错误的是:
uuidvar = uuid4()
mytable.select().where(mytable.c.uuidColumn == uuidvar) #where mytable is of type sqlalchemy.Table
错误:
File "/Users/remi/PycharmProjects/my_venv/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 448, in process
value = _python_UUID(value)
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/uuid.py", line 137, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
sqlalchemy.exc.StatementError: (builtins.AttributeError) 'UUID' object has no attribute 'replace' [SQL: 'SELECT col1, col2, col3 FROM myTable \nWHERE uuidColumn = '%(uuidColumn_1)s']
使用这样的表:
CREATE TABLE myTable (
uuidColumn uuid primary key default uuid_generate_v4()
col1 text,
col2 text,
col3 text
)
并在python中生成表:
metadata = MetaData(engine=db) # db is created by "create_engine", nothing fancy
myTable = Table('myTable', metadata, autoload=True)
答案 0 :(得分:1)
我有类似的问题。要解决此错误,您需要使用as_uuid = True定义uuidColumn。像这样:
from sqlalchemy.dialects import postgresql
uuidColumn = db.Column(postgresql.UUID(as_uuid=True))
有关详细信息,请参阅Michael Bayer的评论:https://bitbucket.org/zzzeek/sqlalchemy/issues/3323/in-099-uuid-columns-are-broken-with