pythonsqlalchemyflask-admin

ImportError: cannot import name 'ASSOCIATION_PROXY' from 'sqlalchemy.ext.associationproxy'


I am working on a small flask API using flask-admin and flask-sqlalchemy. The api runs well but whenever I install a new package I am faced with an error.

Error: While importing 'app', an ImportError was raised:

Traceback (most recent call last):
  File "/Users/brandoncreed/.local/share/virtualenvs/management-System-Wyc_ba1N/lib/python3.8/site-packages/flask/cli.py", line 218, in locate_app
    __import__(module_name)
  File "/Users/brandoncreed/Desktop/Brandon Coding Projects/management-System/src/app.py", line 12, in <module>
    from api.admin import setup_admin
  File "/Users/brandoncreed/Desktop/Brandon Coding Projects/management-System/src/api/admin.py", line 5, in <module>
    from flask_admin.contrib.sqla import ModelView
  File "/Users/brandoncreed/.local/share/virtualenvs/management-System-Wyc_ba1N/lib/python3.8/site-packages/flask_admin/contrib/sqla/__init__.py", line 2, in <module>
    from .view import ModelView
  File "/Users/brandoncreed/.local/share/virtualenvs/management-System-Wyc_ba1N/lib/python3.8/site-packages/flask_admin/contrib/sqla/view.py", line 18, in <module>
    from flask_admin.contrib.sqla.tools import is_relationship
  File "/Users/brandoncreed/.local/share/virtualenvs/management-System-Wyc_ba1N/lib/python3.8/site-packages/flask_admin/contrib/sqla/tools.py", line 11, in <module>
    from sqlalchemy.ext.associationproxy import ASSOCIATION_PROXY
ImportError: cannot import name 'ASSOCIATION_PROXY' from 'sqlalchemy.ext.associationproxy' (/Users/brandoncreed/.local/share/virtualenvs/management-System-Wyc_ba1N/lib/python3.8/site-packages/sqlalchemy/ext/associationproxy.py)

I am not sure what is causing this but it seems there is a conflict between flask_admin and sqlalchemy after the new package is installed. I deleted the pipfile.lock in my virtual environment and ran pipenv install to see if the new file would work but it did not. I also tried uninstalling the new package to see if it would resolve the issue but the same error still persists. I am wondering if it was to do with updating either flask-admin or sqlalchemy.


Solution

  • This issue has been fixed in Flask-Admin v1.6.1, which can be installed like this:

    python3 -m pip install --upgrade flask-admin
    

    Flask-Admin versions earler than v1.6.1 are not compatible with SQLAlchemy 2.0. The was an issue for this specific problem. The recommended workaround was to install an earlier version of SQLALchemy, for example:

    python3 -m pip install --upgrade 'sqlalchemy<2.0'