[英]Python - SQLAlchemy using exists() to check if data already in table?
So I have the following Feeds class 所以我有以下Feed类
class Feeds(DeclarativeBase):
__tablename__ = "rawFeeds"
id = Column(Integer, primary_key=True)
link = Column('link', String)
date = Column('date', String)
source = Column('source', String)
def __init__(self, link, date, source):
self.link = link
self.date = date
self.source = source
and I want to run a check to see if data already exists in a table (to avoid adding duplicates). 并且我想进行检查以查看表中是否已经存在数据(以避免添加重复项)。 I'm using the following statement, but no matter what I try it always produces a True result
我正在使用以下语句,但是无论我尝试什么,它总是会产生True结果
if session.query(exists().where(Feeds.link==item['link'][0])):
print "Already exists"
else:
#add to database
Can anyone help me figure out what I'm doing wrong? 谁能帮我弄清楚我在做什么错? I've read the SQLAlchemy docs over and over, but I can't figure out what the error here is.
我已经读过一遍又一遍的SQLAlchemy文档,但是我不知道这里是什么错误。 Thanks.
谢谢。
session.query() returns a Query object as per the documentation: session.query()根据文档返回Query对象:
http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html
This will mean that your if statement will always evaluate to true. 这意味着您的if语句将始终为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.