title: Checking for non-existent optional tables
body: <p>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 <code>NoSuchTableError</code></p>
  
  <pre><code>try:
      x = session.query(ABC.name.label('sig_name'),func.count('*').label('count_')).join(DEF).join(MNO).filter(MNO.relevance &gt;= relevance_threshold).group_by(DEF.signature).order_by(desc('count_')).all()[:val]
  except NoSuchTableError:
      x = [-1,]
  </code></pre>
  
  <p>But on executing this statement, I get a ProgrammingError</p>
  
  <pre><code>ProgrammingError: (ProgrammingError) (1146, "Table 'db.mno' doesn't exist")
  </code></pre>
  
  <p>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?</p>
  
  <p><strong>EDIT1</strong></p>
  
  <p>Since this is part of my webapp, the model of DB is in <code>models.py</code> (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 <code>models.py</code>)</p>
  
  <pre><code>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'),)
  </code></pre>
  
  <p>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?</p>
  
  <p>Note: In this case I would have to try if...else rather than 'ask for forgiveness'</p>