- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在根据关系在 view_state
中提取用户/视频时遇到问题。
使用Python 2.7
SQLALCHEMY
和CRUD方法
测试.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/
我已经开始使用炼金术 API,但我想知道是否是否有任何科学出版物解释用于从文本中提取关键字和概念的模型? 也是根据这个回答Is there way to influence AlchemyAPI se
我正在 SQLAlchemy 中寻找一种方法来进行批量处理 INSERT其行是查询的结果。我知道 session 具有功能 add它可用于添加单个对象,但我似乎无法找到它如何与子查询一起使用。 我知道
在使用 Alchamy 的实体 API 时收到“截断过大文本内容”的警告 - 有谁知道字符限制 - 我在他们的支持页面上找不到。 最佳答案 文本限制为50kb,在this page的底部有点隐藏. 关
我在 SQLAlchemy 中使用外键引用进行继承时遇到了困难。 我有一个drives表格看起来像这样 id作为主键: Column | Type |
我正在使用 alchemy 来编译 C 代码。 这就是我编译的方式 gcc oggvorbis.c -O3 -Wall -swc oggvorbis.swc -lvorbis 我收到错误 llvm-g
我尝试将 Alchemy NLP API 实现到我的 Android 项目中,但是我遇到了一些困难。从http://www.alchemyapi.com/developers/sdks/下载SDK时你
我似乎一辈子都无法安装 whoosh alchemy。引用:http://pythonhosted.org/Flask-WhooshAlchemy/ .不管怎样,我在关注http://blog.mig
我将 SQL Alchemy 的声明式风格与 PostgreSQL 数据库结合使用。我有一个管理 Claim 对象的 ClaimAccount 。像这样的东西: class Claim(MyBase)
我有用户模型,描述如下:- class User(db.Model, object): __tablename__ = "users" id = db.Column(db.Intege
我有一个这样定义的模型: class Article(db.Model): id = db.Column(db.Integer, primary_key=True) authors =
我是 python 和 SQLALCHEMY 的新手,我遇到了这个疑问,我们是否可以根据同一表列的单元格值过滤表的行。示例: Sbranch=value result=Transaction.quer
假设我得到一个这样的模型实例: instance = session.query(MyModel).filter_by(id=1).first() 如何删除该行?有没有特殊的调用方法? 最佳答案 好的
我打算制作一个 AIR 应用程序,但我需要执行一个外部应用程序,并且由于 Adobe AIR 中的安全限制......我在想为什么不尝试通过编写一些 C 代码来绕过它来执行类似 System 的操
我们当前的项目在很大程度上依赖于 SQL Alchemy 来创建表/插入数据。我们想切换到 timescaledb 的超表,但似乎推荐的创建超表的方法是执行 create_hypertable 命令。
我创建了以下方法来使用 SQL Alchemy 和 Pyodbc 连接到 SQL Server。 def getDBEngine(server, database): Params = url
我是 sql alchemy 的新手,我想计算表中的唯一行数,并输出唯一行以及该行的副本数。假设我有一个这样的表 Table A -------------- id address 现在我想获取该表的
我是 stackoverflow 新手。 我目前正在开发一个 WebSocket 项目,并使用一个名为 Alchemy-Websockets 的组件。 我的问题是当我尝试像这样使用 Alchemy.W
我是 sql alchemy 的新手,我想计算表中的唯一行数,并输出唯一行以及该行的副本数。假设我有一个这样的表 Table A -------------- id address 现在我想获取该表的
我们想在 sql alchemy 类上申请权限。我们有一个表,其中包含用户可以访问的映射。 创建表访问 ( 用户名 varchar(256), 策略 ID 整数 ) 我们的大多数表都包含 strate
我有以下场景 partner = query().filter(Partners.id == partner_id).first() if partner != None: partner.c
我是一名优秀的程序员,十分优秀!