gpt4 book ai didi

python - SQL ALCHEMY 关系问题 (M :1)

转载 作者:行者123 更新时间:2023-12-01 03:33:32 25 4
gpt4 key购买 nike

我在根据关系在 view_state 中提取用户/视频时遇到问题。

使用Python 2.7 SQLALCHEMYCRUD方法

测试.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='a@gmail.org', institution='foo', residency_year='None')>, <Video(videoname='module1', length='8.0', url='https://vimeo.com/138326103')>)]

最佳答案

如果您仔细查看回溯消息,您会发现您发送的不是用户名,而是一个类作为参数,但尝试将其与字符串匹配。这实际上就是你的程序所做的。您首先选择用户类别和视频类别并将这些类别传递给 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()

汉努

关于python - SQL ALCHEMY 关系问题 (M :1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40594236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com