过滤False列值时返回True

时间:2019-03-08 02:40:33

标签: python python-3.x sqlalchemy mariadb

我有两个表,在第二个表具有bit列的地方进行左连接。过滤器返回该位为False的列。 但是当我稍后在循环中检查该值时,它显示为true。

from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from sqlalchemy import Column, String, Integer, Boolean, ForeignKey, or_
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm.exc import NoResultFound
# from sqlalchemy.exc import DataError
Base = declarative_base()

class BoardGame(Base):
    __tablename__ = 'game_detail'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    description = Column(String)
    process = relationship('Process', uselist=False, back_populates='game_detail')

    def __repr__(self):
        return "<BoardGame(id='{}', name='{}', description='{}'".format(
            self.id, self.name, self.description
        )

class Process(Base):
    __tablename__ = 'process'
    game_id = Column(Integer, ForeignKey('game_detail.id'), primary_key=True)
    description_bigram = Column(Boolean)
    game_detail = relationship('BoardGame', back_populates='process')

sa_engine = create_engine('mysql+pymysql://bgg:blahblahblah@localhost:49000/boardgamegeek?charset=utf8mb4', pool_recycle=3600)
session = Session(bind=sa_engine)

# Set the scripts execution range of data
maximum_games_to_process = 1

for game in session.query(BoardGame.id, Process.description_bigram).join(Process, isouter=True).filter(Process.description_bigram.is_(False)).limit(maximum_games_to_process):
    print('description_bigram', type(game.description_bigram), game.description_bigram)
    print(game)

运行以上内容可以吸引我

description_bigram <class 'bool'> True
(1, True)

但是查看HeidiSQL中的数据会发现其他情况。

game_id;description_bigram
1;0
2;1
3;0
4;0
5;1

这是过程表的创建代码,如HeidiSQL中所示

CREATE TABLE `process` (
    `game_id` INT(11) NOT NULL,
    `description_bigram` BIT(1) NOT NULL,
    PRIMARY KEY (`game_id`),
    CONSTRAINT `FK__GAME_ID_DESCRIPTION` FOREIGN KEY (`game_id`) REFERENCES `game_detail` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB

我看到一些有关布尔列和people have change their tables and code to work with integers instead的SQLAlchemy问题的文章(链接仅与我的情况相似)。我确定可以使用,但是我还有其他程序可以与Bit列和表布尔定义一起使用,因此我不确定为什么这个程序不能正常工作。我可以看一下里面的东西吗?

这是SQLAlchemy创建的查询

SELECT game_detail.id AS game_detail_id, process.description_bigram AS process_description_bigram
FROM game_detail LEFT OUTER JOIN process ON game_detail.id = process.game_id
WHERE process.description_bigram IS false

我尝试了一些尝试来获取该列以注册其正确的值,而我一直以它为真而结束。

Python是3.6.5版本; SQLAlchemy是1.2.7; 10.3.13-MariaDB

0 个答案:

没有答案