0

I am learning SQLAlchemy and trying to check if a record exists in postgreSQL database using SQLAlchemy. There are many similar question here, but I am stuck. Here is my query:

ret = session.query(exists().where(COMPANY.name == 'MyTestCompany')).scalar()

"COMPANY" is table name , when I run it, I get error:

"NameError: name 'COMPANY' is not defined"

Should I somehow "register" COMPANY table as an object in current session or the problem lies somewhere else?

1 Answer 1

1

The error you are getting means there is no COMPANY variable defined in your python code.

Did you use any other ORM features? Usually you would need to create a Company Model to run a query like this. A model would look something like this:

class Company(Base):
    __tablename__ = 'company'
    id = Column(Integer, primary_key=True)
    name = Column(String)

Check out how they create a User model in this tutorial

If you want to interact with SQL directly without ORM features you might be interested in SQLAlchemy Core

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

Comments

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.