1

查询似乎没问题,当我将布尔值过滤为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 扩展的 SQLAlchemy,并且一切正常,所以它看起来像 C 扩展中的一个错误。我要向他们报告这件事,看看他们怎么说。

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

4

1 回答 1

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 版本以及完整的重现测试用例...谢谢。

于 2013-10-25T17:33:01.967 回答