JDGD
JDGD

Reputation: 543

Python - SQLAlchemy using exists() to check if data already in table?

So I have the following Feeds class

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

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. Thanks.

Upvotes: 1

Views: 5755

Answers (1)

Owen
Owen

Reputation: 1736

session.query() returns a Query object as per the documentation:

http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html

This will mean that your if statement will always evaluate to true.

Upvotes: 2

Related Questions