Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7876625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:11:44+00:00 2026-06-03T03:11:44+00:00

I am using SQLAlchemy + Pyramid to operate on my database. However, there are

  • 0

I am using SQLAlchemy + Pyramid to operate on my database. However, there are some optional tables which are not always expected to be present in the DB. So while querying them I try to catch such cases with the NoSuchTableError

try:
    x = session.query(ABC.name.label('sig_name'),func.count('*').label('count_')).join(DEF).join(MNO).filter(MNO.relevance >= relevance_threshold).group_by(DEF.signature).order_by(desc('count_')).all()[:val]
except NoSuchTableError:
    x = [-1,]

But on executing this statement, I get a ProgrammingError

ProgrammingError: (ProgrammingError) (1146, "Table 'db.mno' doesn't exist")

Why does SQLAlchemy raise the more general ProgrammingError instead of the more specific NoSuchTableError? And if this is indeed expected behaviour, how do I ensure the app displays correct information depending on whether tables are present/absent?

EDIT1

Since this is part of my webapp, the model of DB is in models.py (under my pyramid webapp). I do have a setting in my .ini file that asks user to select whether additional tables are available or not. But not trusting the user, I want to be able to check for myself (in the views) whether table exists or not. The contentious table is something like (in models.py)

class MNO(Base):
    __tablename__="mno"
    id=Column(Integer,primary_key=True,autoincrement=True)
    sid=Column(Integer)
    cid=Column(mysql.MSInteger(unsigned=True))
    affectability=Column(Integer)
    cvss_base=Column(Float)
    relevance=Column(Float)
    __table_args__=(ForeignKeyConstraint(['sid','cid',],['def.sid','def.cid',]),UniqueConstraint('sid','cid'),)

How and Where should the check be made so that a variable can be set (preferably during app setup) which tells me whether the tables are present or not?

Note: In this case I would have to try if…else rather than ‘ask for forgiveness’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:11:45+00:00Added an answer on June 3, 2026 at 3:11 am

    According to the sqlalchemy docs, a NoSuchTableError is only thrown when “SQLAlchemy [is] asked to load a table’s definition from the database, but the table doesn’t exist.” You could try loading a table’s definition, catching the error there, and doing your query otherwise.

    If you want to do things via “asking for forgiveness”:

    try:
        table = Table(table_name, MetaData(engine)) 
    except NoSuchTableError:
        pass
    

    Alternatively, you could just check whether the table exists:

    Edit:

    Better yet, why don’t you use the has_table method:

    if engine.dialect.has_table(connection, table_name):
        #do your crazy query
    

    Why don’t you use Inspector to grab the table names first?

    Maybe something like this:

    from sqlalchemy import create_engine
    from sqlalchemy.engine import reflection
    #whatever code you already have
    engine = create_engine('...')
    insp = reflection.Inspector.from_engine(engine)
    table_name = 'foo'
    table_names = insp.get_table_names()
    if table_name in table_names:
        x = session.query(ABC.name.label('sig_name'),func.count('*').label('count_')).join(DEF).join(MNO).filter(MNO.relevance >= relevance_threshold).group_by(DEF.signature).order_by(desc('count_')).all()[:val]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating the web app using Pyramid-1.2.1 with SQLAlchemy as database backend. Now I
I'm currently developing an application which connects to a database using sqlalchemy. The idea
I've been trying to map an object to a database using SQLAlchemy but have
Is it possible to have the postgres database dump(pg_dump) using SQLAlchemy? i can get
I'm using Pyramid and SQLAlchemy, but the following simplified code: u = u\u201C m
I'm using SQLAlchemy as the ORM within an application i've been building for some
I am using SqlAlchemy, a python ORM library. And I used to access database
I have recently started using SQLALCHEMY to query a my-sql database. I want to
I'm using SqlAlchemy to store some objects with a DateTime field: my_date = Field(DateTime())
I create a table in database using sqlalchemy and now want to make a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.