'No database selected error after explicit declaration_base

I'm having this issue, where sqlalchemy does not recognize the database, even though it is declared with declarative_base. After trying to run a simple query of session.query(AppGeofencing).all() I get sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1046, 'No database selected').

The table is declared as

Base = declarative_base()
AppBase = declarative_base(metadata=MetaData(schema='app'))

class AppGeofencing(AppBase):

    __tablename__ = 'geofencing'

    id = Column(INTEGER, primary_key=True, autoincrement=True)
    name = Column(VARCHAR(45))
    polygon = Column(Geometry('POLYGON'))

    def __init__(self, name=None, polygon=None):
        self.name = name
        self.polygon = polygon

The case is only with this table, because I have also done similarly for other tables, and they work just fine.

After enabling the logging for sqlalchemy I can see that is does indeed create the correct query

INFO:sqlalchemy.engine.Engine:SELECT app.geofencing.id AS app_geofencing_id, app.geofencing.name AS app_geofencing_name, ST_AsEWKB(app.geofencing.polygon) AS app_geofencing_polygon 
FROM app.geofencing

but somehow it cannot determine the database to use? Does anyone have any idea, what could cause such issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source