在SQL Alchemy中更新boolean(使用CRUD)

时间:2016-11-14 18:01:28

标签: python-2.7 sqlalchemy boolean crud traceback

我正在尝试将布尔语句从默认False更新为True。这里的想法是用户观看一些视频,更新方法从False更改为True

我正在努力编写update_view_state方法。我已经尝试了点符号(我承认这是新手)并且没有成功。

使用Python 2.7 SQLALCHEMYCRUD method

class View_State(Base):
    __tablename__ = 'view_states'
    #more code
    completed = Column(Boolean, default=False) #have to set default

    def __init__(self, user, video):
        self.completed = False

更新代码:

def update_view_state(self, username, videoname, completed):
    #update Boolean completed status to 'complete = True'
    update_view = self.session.query(View_State).\
    filter(View_State.user.has(username == username)).\
    filter(View_State.video.has(videoname == videoname)).one()
    if update_view:
        print 'update view found: ', update_view.completed
        update_view.completed.append(completed)
        self.session.commit()
        return update_view

Test.py

api.update_view_state('ack', 'module1', True)

追溯:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/ack/code/enotss_copy/NotssDB/notssdb/test/test.py", line 296, in test_crud_operations
    api.update_view_state('ack', 'module1', True)
  File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/convenience.py", line 35, in update_view_state
    return super(ConvenienceAPI, self).update_view_state(user, video, completed)
  File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/object.py", line 634, in update_view_state
    filter(View_State.user.has(username == username)).\
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 442, in has
    return self.operate(PropComparator.has_op, criterion, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 175, in operate
    return op(self.comparator, *other, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 383, in has_op
    return a.has(b, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1172, in has
    return self._criterion_exists(criterion, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1081, in _criterion_exists
    criterion = criterion._annotate(
AttributeError: 'bool' object has no attribute '_annotate'

使用以下ANSWER:

追溯:

ERROR: notssdb.test.test.test_crud_operations
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/ack/code/enotss_copy/NotssDB/notssdb/test/test.py", line 295, in test_crud_operations
    api.update_view_state('ack', 'module1', True)
  File "/Users/ack/code/enotss_copy/NotssDB/notssdb/api/object.py", line 628, in update_view_state
    filter(View_State.user.has(username == username)).\
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 442, in has
    return self.operate(PropComparator.has_op, criterion, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 175, in operate
    return op(self.comparator, *other, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 383, in has_op
    return a.has(b, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1172, in has
    return self._criterion_exists(criterion, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1081, in _criterion_exists
    criterion = criterion._annotate(
AttributeError: 'bool' object has no attribute '_annotate'

1 个答案:

答案 0 :(得分:1)

在回复您的评论时,我认为我只是发布答案以提供详细信息。希望这有助于您清理并正常运行。

def update_view_state(self, username, videoname, completed):
    #update Boolean completed status to 'complete = True'
    update_view = self.session.query(View_State).\
    filter(View_State.user.has(username == username)).\
    filter(View_State.video.has(videoname == videoname)).one()
    if update_view:
        print 'update view found: ', update_view.completed
        update_view.completed = True # Simply reassign it like a variable
        self.session.add(update_view) # Add it to the session
        self.session.commit() # And commit
        return update_view

我在代码中使用了self.session,因为这是您正在使用的内容。我从未以这种方式设置我的会话,因此我只使用过session.commit()session.add(),但我试图保持与您的会话保持一致。已经发布了,因为代码的其他一些未发布的部分可能需要这些内容。

我已在此处直接更新为布尔值True,但我发现您已使用了您可能更喜欢使用的功能参数completed。在这种情况下,只需使用update_view.completed = completed即可。也许可以考虑重命名该参数,以避免与数据库中具有相同名称的列混淆。

正如您所问,如果我们将参数completed重命名为status_change,它将如下所示:

def update_view_state(self, username, videoname, status_change):
    #update Boolean completed status to 'complete = True'
    update_view = self.session.query(View_State).\
    filter(View_State.user.has(username == username)).\
    filter(View_State.video.has(videoname == videoname)).one()
    if update_view:
        print 'update view found: ', update_view.completed
        update_view.completed = status_change # Simply reassign it like a variable
        self.session.add(update_view) # Add it to the session
        self.session.commit() # And commit
        return update_view

在这种情况下,如果您想来回切换,则可以使用该功能将update_view.completed设置为TrueFalse。这样做只是将您选择的布尔值作为参数传递给函数。如果您只想使用此函数将其设置为True,则可以完全删除status_change参数,并使用第一个示例中的原始布尔值。