SQLAlchemy无法使用mysql将boolean转换为int

时间:2013-10-25 13:55:03

标签: python mysql sql sqlalchemy

查询似乎没问题,并且当我将布尔值过滤为 False 时工作正常,但是当查询返回布尔值为 True 的项时,它会出现此异常

query = session.query(URL).filter_by(status_code=200,
                                     content_type='text/html',
                                     is_phish_whitelist=True,
                                     is_phish_google_safebrowsing=None,
                                     is_malware_google_safebrowsing=None)


Traceback (most recent call last):
  File "/home/user/PycharmProjects/poc/collector/verify_googlesafe.py", line 25, in <module>
    phish_urls = phish_query.all()
  File "/home/user/Envs/poc/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2237, in all
    return list(self)
  File "/home/user/Envs/poc/local/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 72, in instances
    rows = [process[0](row, None) for row in fetch]
  File "/home/user/Envs/poc/local/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 447, in _instance
    populate_state(state, dict_, row, isnew, only_load_props)
  File "/home/user/Envs/poc/local/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 301, in populate_state
    populator(state, dict_, row)
  File "/home/user/Envs/poc/local/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py", line 150, in fetch_col
    dict_[key] = row[col]
ValueError: int_to_boolean only accepts None, 0 or 1

编辑:

我已经安装了没有C Extensions的SQLAlchemy,并且正常工作,所以它看起来像C扩展中的一个bug。我要向他们报告,看看他们说了什么。

pip install --global-option =' - without-cextensions'SQLAlchemy

1 个答案:

答案 0 :(得分:0)

无法重现:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)

    data = Column(Boolean)

e = create_engine("mysql://scott:tiger@localhost/test", echo=True)
Base.metadata.drop_all(e)
Base.metadata.create_all(e)

sess = Session(e)

sess.add_all([A(data=True), A(data=False)])
sess.commit()

print sess.query(A).filter_by(data=True).all()
print sess.query(A).filter_by(data=False).all()

0.8.2输出:

CREATE TABLE a (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    data BOOL, 
    PRIMARY KEY (id), 
    CHECK (data IN (0, 1))
)


2013-10-25 13:30:41,146 INFO sqlalchemy.engine.base.Engine ()
2013-10-25 13:30:41,177 INFO sqlalchemy.engine.base.Engine COMMIT
2013-10-25 13:30:41,179 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-10-25 13:30:41,179 INFO sqlalchemy.engine.base.Engine INSERT INTO a (data) VALUES (%s)
2013-10-25 13:30:41,179 INFO sqlalchemy.engine.base.Engine (1,)
2013-10-25 13:30:41,181 INFO sqlalchemy.engine.base.Engine INSERT INTO a (data) VALUES (%s)
2013-10-25 13:30:41,181 INFO sqlalchemy.engine.base.Engine (0,)
2013-10-25 13:30:41,181 INFO sqlalchemy.engine.base.Engine COMMIT
2013-10-25 13:30:41,182 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-10-25 13:30:41,183 INFO sqlalchemy.engine.base.Engine SELECT a.id AS a_id, a.data AS a_data 
FROM a 
WHERE a.data = true
2013-10-25 13:30:41,183 INFO sqlalchemy.engine.base.Engine ()
[<__main__.A object at 0x101ea1350>]
2013-10-25 13:30:41,184 INFO sqlalchemy.engine.base.Engine SELECT a.id AS a_id, a.data AS a_data 
FROM a 
WHERE a.data = false
2013-10-25 13:30:41,184 INFO sqlalchemy.engine.base.Engine ()
[<__main__.A object at 0x101ea14d0>]

0.7.9输出:

CREATE TABLE a (
    id INTEGER NOT NULL AUTO_INCREMENT, 
    data BOOL, 
    PRIMARY KEY (id), 
    CHECK (data IN (0, 1))
)


2013-10-25 13:32:00,079 INFO sqlalchemy.engine.base.Engine ()
2013-10-25 13:32:00,105 INFO sqlalchemy.engine.base.Engine COMMIT
2013-10-25 13:32:00,106 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-10-25 13:32:00,107 INFO sqlalchemy.engine.base.Engine INSERT INTO a (data) VALUES (%s)
2013-10-25 13:32:00,107 INFO sqlalchemy.engine.base.Engine (1,)
2013-10-25 13:32:00,107 INFO sqlalchemy.engine.base.Engine INSERT INTO a (data) VALUES (%s)
2013-10-25 13:32:00,108 INFO sqlalchemy.engine.base.Engine (0,)
2013-10-25 13:32:00,108 INFO sqlalchemy.engine.base.Engine COMMIT
2013-10-25 13:32:00,108 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-10-25 13:32:00,109 INFO sqlalchemy.engine.base.Engine SELECT a.id AS a_id, a.data AS a_data 
FROM a 
WHERE a.data = %s
2013-10-25 13:32:00,109 INFO sqlalchemy.engine.base.Engine (1,)
[<__main__.A object at 0x10111a250>]
2013-10-25 13:32:00,110 INFO sqlalchemy.engine.base.Engine SELECT a.id AS a_id, a.data AS a_data 
FROM a 
WHERE a.data = %s
2013-10-25 13:32:00,110 INFO sqlalchemy.engine.base.Engine (0,)
[<__main__.A object at 0x1011292d0>]

请提供SQLAlchemy版本以及完整的再现测试用例...谢谢。