session.query(Model).all() - skip/ignore non-existing columns

Viewed 78

I'm connecting to multiple remote databases (of multiple types) (that are created by website users) using SQLAlchemy.

I've declared a model that should describe the table but users sometimes forget/skip to add some columns to their table. That causes errors when getting data from the DB.

pymysql.err.OperationalError) (xxx, "Unknown column 'table.phone' in 'field list'")

Model:

class Employee(Base):
    __tablename__ = 'contacts'

    id = Column(Integer, primary_key=True)
    first_name = Column(String(1000))
    last_name = Column(String(1000))
    email = Column(String(1000))
    phone = ....

Getting data:

session: Session = Session(bind=engine)
session.query(Employee).all()

So I want to "skip" non-existing columns and get only those that exist.

Is that possible?

0 Answers
Related