2015-04-22 38 views
2

我希望這是我最後一個有關這個寵物項目的問題。Sqlalchemy - 使用過濾器更新行產生列表索引錯誤

我想用當前時間更新表Clockclock(column = time_out)。我想通過用UUID4創建的UUID進行選擇。當我運行代碼時,我得到ListIndex超出範圍錯誤,在query.py(Sqlalchemy的一部分)中的函數。我在我的代碼下面列出了相當短的功能。

我的代碼:

out = session.query(). \ 
     filter(Clocktime.p_uuid == p_uuid). \ 
     update(Clocktime.time_out == datetime.datetime.now()) 
    session.add(out) 
    session.commit() 

SQLAlchemy的代碼拋出錯誤:

def _entity_zero(self): 
    return self._entities[0] 

的SQLAlchemy的代碼是非常複雜的,所以我沒有通過它看上去還,但我的理論是,它不」不喜歡我的過濾器出於某種原因。無論如何,我無法弄清楚 - 你們有沒有任何想法?

回答

2

我不認爲你的代碼正在做你想做的事情。 有兩種方法可以讓你想更新:

1)直接對數據庫進行:

upd = (session.query(Clocktime) 
     .filter(Clocktime.p_uuid == p_uuid) 
     .update({"time_out": datetime.datetime.now()}) 
     ) 
print("# of updated rows = {}".format(upd)) 
# session.commit() 

2)負載對象(S),更新的價值,並提交會議

upd = (session.query(Clocktime) 
     .filter(Clocktime.p_uuid == p_uuid) 
     ) 

# assuming there should be exactly one object for given p_uuid  
clockTime = upd.one() 
clockTime.time_out = datetime.datetime.now() 
session.commit() 
相關問題