1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, in case anyone else has this problem I modified the If statement to the following: session.query(Feeds).filter(Feeds.link==item['link'][0]).scalar()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.