2016-11-14 81 views
0

我在根據關係拉起view_state中的用戶/視頻時遇到問題。SQL ALCHEMY關係問題(M:1)

使用Python 2.7SQLALCHEMYCRUD method

Test.py

def test_crud_operations(): 

    api = ConvenienceAPI() 
    api.create_view_state('module1', 'ack') 
    api.retrieve_view_state('ack') 
    api.update_view_state('ack', 'module1') 

代碼:

#base.py 
    class View_State(Base): 
     __tablename__ = 'view_states' 

     id = Column(Integer, primary_key=True) 
     timestamp = Column(DateTime, default=datetime.utcnow) 
     time_update = Column(DateTime, onupdate=datetime.utcnow) 
     completed = Column(Boolean, default=False) #have to set default 

     video_id = Column(Integer, ForeignKey('videos.id')) 
     video = relationship('Video', backref='view_states') 

     user_id = Column(Integer, ForeignKey('users.id')) 
     user = relationship('User', backref='view_states') 

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

     def __repr__(self): 
      return "<View_State(timestamp='%s', time_update='%s', completed='%s', video='%s', user='%s')>" % (self.timestamp, self.time_update, self.completed, self.video, self.user) 

    #object.py 

     # View State CRUD 

      def update_view_state(self, username, videoname): 
       #update Boolean completed status to 'complete = True' 
       update_completed = self.session.query(View_State).\ 
       filter(View_State.user.has(User.username == username)).\ 
       filter(View_State.video.has(Video.videoname == videoname)).one() 
       print 'retrieved from update complete: ', update_completed 
       if update_completed: 
        completed = True 
        temp = update_completed.completed 
        print 'changed status: ', temp 
        return update_completed 
        self.session.commit() 

    #convenience.py 

     def update_view_state(self, username, videoname): 
      user = self.retrieve_user(username=username) 
      video = self.retrieve_video(videoname) 
      return super(ConvenienceAPI, self).update_view_state(user, video) 

回溯:

InterfaceError: (sqlite3.InterfaceError) Error binding parameter 0 - probably unsupported type. [SQL: u'SELECT view_states.id AS view_states_id, view_states.timestamp AS view_states_timestamp, view_states.time_update AS view_states_time_update, view_states.completed AS view_states_completed, view_states.video_id AS view_states_video_id, view_states.user_id AS view_states_user_id \nFROM view_states \nWHERE (EXISTS (SELECT 1 \nFROM users \nWHERE users.id = view_states.user_id AND users.username = ?)) AND (EXISTS (SELECT 1 \nFROM videos \nWHERE videos.id = view_states.video_id AND videos.videoname = ?))'] [parameters: (<User(username ='ack', firstname ='A', lastname ='cr', email='[email protected]', institution='foo', residency_year='None')>, <Video(videoname='module1', length='8.0', url='https://vimeo.com/138326103')>)] 

回答

1

如果仔細查看回溯消息,則不是發送用戶名,而是將類作爲參數發送,但嘗試將其與字符串進行匹配。這實際上是你的程序的功能。您首先選擇用戶類和視頻類,並將這些類傳遞給update_view_state。

,如果你改變了這一行,會發生什麼:

return super(ConvenienceAPI, self).update_view_state(user, video) 

return super(ConvenienceAPI, self).update_view_state(user.username, 
                video.videoname) 

或者你可以改變你update_view_state上的關係,而不是操作:

filter(View_State.user.has(user == username)).\ 
filter(View_State.video.has(video == videoname)).one() 

哈努

+0

wha我試圖做的是按類查找(在其他地方工作),但隨後在'view_state'中更新布爾值...我認爲我的更新代碼是不準確的。 – thesayhey

+1

我修改了我的答案以包含替代方案。您的用戶名和視頻名稱參數被命名爲「錯誤」,因爲您沒有發送名稱而只是發送了一個類。不過,這些類可以與數據類中的關係進行比較。 – Hannu

+0

工作!我打算比較這種關係。但現在傳遞更新的布爾值不起作用。你可以看看'update_view_state'代碼嗎? 'ValueError:檢測到雙向屬性衝突:將對象<用戶在0x108a22550>傳遞給屬性「View_State.video」通過backref「User.view_states」在屬性「View_State.user」觸發修改事件。' – thesayhey