Sqlalchemy - 查询与Key:Value Pair不匹配的所有对象

时间:2016-01-26 17:27:17

标签: python sqlalchemy

我有一本看起来像这样的字典

{'NYDEARZ15': '2016-01-25 16:20:15', 'NYDE412': '2016-01-25 16:20:16'
: '2016-01-25 16:20:15', 'NLE-512': '2016-01-25 16:20:14', 'NYDEZ28':
5 16:20:15', 'NYNLE645': '2016-01-26 10:45:44', 'NYDE410': '2016-01-2
, 'NLE-508': '2016-01-25 16:20:14', 'NYNLE500B': '2016-01-25 16:20:17
: '2016-01-25 16:20:16', 'NYDE307': '2016-01-25 16:20:17', 'NYDEZ09':
5 16:20:15', 'NYDE13A': '2016-01-25 16:20:16', 'NYTING10': '2016-01-2
, 'NLE-517': '2016-01-25 16:20:15', 'NYGFX660': '2016-01-25 16:20:15'}

每个键代表computer name,每个值代表"CheckIn Time Stamp"

我想在数据库中查询与computers

不匹配的timestamp列表

有没有办法在单个查询中完成此操作?没有迭代数据库来单独检查每台计算机?简单地检查数据库中是否有已更改的计算机列表似乎相当昂贵,然后再次查询所有其他信息。

我使用scoped_session进行查询,而不是表达式语言。

我尝试过:

computers = Session.query(Computer).filter(Computer.last_checked_in != check_in_dict[Computer.name]).all()

结果:

KeyError: <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x000000000 4A3C620>

似乎不允许使用schema属性作为字典的引用。

我应该研究哪些想法或概念?

感谢您阅读

编辑:模型参考

class Computer(Base):
    """
    An edit station machine.
    "name" should be the DNS entry for the computer.
    """
    __tablename__ = 'computers'
    id = sqlalchemy.Column(sqlalchemy.Integer,
                           primary_key=True,
                           autoincrement=True
                           )
    name = sqlalchemy.Column(sqlalchemy.String(64))
    short_description = sqlalchemy.Column(sqlalchemy.String(128))
    notes = sqlalchemy.Column(sqlalchemy.Text)
    model = sqlalchemy.Column(sqlalchemy.String(64))
    station_type_id = sqlalchemy.Column(sqlalchemy.Integer,
                                        sqlalchemy.ForeignKey(StationType.id)
                                        )
    station_type = relationship('StationType', backref='Computer')
    room = relationship('Room', backref='Computer')
    current_room_id = sqlalchemy.Column(sqlalchemy.Integer,
                                        sqlalchemy.ForeignKey(Room.id)
                                        )
    dual_boot = sqlalchemy.Column(sqlalchemy.Boolean)
    online = sqlalchemy.Column(sqlalchemy.Boolean)
    visible = sqlalchemy.Column(sqlalchemy.Boolean)
    ip_address = sqlalchemy.Column(sqlalchemy.String(24))
    record_created = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False)

    def __str__(self):
        return str(self.name)

    def __unicode__(self):
        return self.name

    def __repr__(self):
        return str(self.name)

    @property
    def last_active_drive(self):
        try:
            most_recent = max([drive.last_checked_in for drive in self.drives])
            for drive in self.drives:
                if drive.last_checked_in >= most_recent:
                    return drive
        except ValueError as e:

            return None
        return None

    @property
    def active_drive(self):
        """
        Return the active drive.
        """
        try:
            return next(drive for drive in self.drives if drive.is_active)
        except StopIteration:
            return self.last_active_drive

    @property
    def current_workgroup(self):
        """
        Return the workgroup name the current drive is connected to.
        """
        try:
            return self.active_drive.workgroup.shorthand
        except AttributeError:
            return None

    @property
    def last_checked_in(self):
        """
        Return the time the active drive last checked in.
        """
        try:
            return self.active_drive.last_checked_in
        except AttributeError:
            return None


class Drive(Base):
    """
    A drive in an edit station.
    """
    __tablename__ = 'drives'

    id = sqlalchemy.Column(sqlalchemy.Integer,
                           primary_key=True,
                           autoincrement=True
                           )
    name = sqlalchemy.Column(sqlalchemy.String(64))
    computer_id = sqlalchemy.Column(sqlalchemy.Integer,
                                    sqlalchemy.ForeignKey(Computer.id)
                                    )
    computer = relationship('Computer', backref='drives')
    is_active = sqlalchemy.Column(sqlalchemy.Boolean)
    free_space = sqlalchemy.Column(sqlalchemy.BigInteger)
    used_space = sqlalchemy.Column(sqlalchemy.BigInteger)
    total_space = sqlalchemy.Column(sqlalchemy.BigInteger)
    percentage_full = sqlalchemy.Column(sqlalchemy.Float)
    boot_time = sqlalchemy.Column(sqlalchemy.DateTime)

    last_changed_workgroup = sqlalchemy.Column(sqlalchemy.DateTime)
    last_checked_in = sqlalchemy.Column(sqlalchemy.DateTime)


    def update(self, update_dict):
        """
        provide a dictionary that mimics class attributes
        """

        for key in update_dict:
            setattr(self, key, update_dict[key])

    def __unicode__(self):
        return self.name

    @property
    def uptime(self):
        return datetime.datetime.now() - self.boot_time

    def __str__(self):
        return str(self.name)

0 个答案:

没有答案