SQLAlchemy InvalidRequestError: failed to locate name happens only on gunicorn
Question: Okay, so I have the following. In `user/models.py`:

    
    
    class User(UserMixin, SurrogatePK, Model):
        __tablename__ = 'users'
        id = Column(db.Integer, primary_key=True, index=True)
        username = Column(db.String(80), unique=True, nullable=False)
        email = Column(db.String(80), unique=False, nullable=False)
        password = Column(db.String(128), nullable=True)
        departments = relationship("Department",secondary="user_department_relationship_table", back_populates="users")
    

and in `department/models.py`:

    
    
    user_department_relationship_table=db.Table('user_department_relationship_table',
                                 db.Column('department_id', db.Integer,db.ForeignKey('departments.id'), nullable=False),
                                 db.Column('user_id',db.Integer,db.ForeignKey('users.id'),nullable=False),
                                 db.PrimaryKeyConstraint('department_id', 'user_id') )
    
    
    class Department(SurrogatePK, Model):
        __tablename__ = 'departments'
        id = Column(db.Integer, primary_key=True, index=True)
        name = Column(db.String(80), unique=True, nullable=False)
        short_name = Column(db.String(80), unique=True, nullable=False)
        users = relationship("User", secondary=user_department_relationship_table,back_populates="departments")
    

Using the flask development server locally this works totally fine. However,
once I deploy to the standard python buildpack on heroku, the `cpt/app.py`
loads both modules to register their blueprints:

    
    
    from cpt import (
            public, user, department
            )
    
    ...
    
    
    def register_blueprints(app):
        app.register_blueprint(public.views.blueprint)
        app.register_blueprint(user.views.blueprint)
        app.register_blueprint(department.views.blueprint)
        return None
    

and eventually errors out with the following:

> sqlalchemy.exc.InvalidRequestError: When initializing mapper
> Mapper|User|users, expression 'user_department_relationship_table' failed to
> locate a name ("name 'user_department_relationship_table' is not defined").
> If this is a class name, consider adding this relationship() to the class
> after both dependent classes have been defined.

I'd like to know if there's a better way to organize these parts to avoid this
error obviously, but I'm more curious why this organization works fine on the
development server but blows up something fierce on gunicorn/heroku.


Answer: Well I can't explain the discrepancy between heroku and the dev server, but I
got the error to go away by changing the Department mode from

    
    
    users = relationship("Department",secondary="user_department_relationship_table", back_populates="users")
    

to

    
    
    users = relationship("User", secondary=user_department_relationship_table, backref="departments")
    

which sets up the User model automatically which in turn means I can delete
any mention of Department and the relationship table on that end.

¯\\_(ツ)_/¯

