pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.8k stars 1.58k forks source link

ImportError: Cannot import name 'ASSOCIATION_PROXY' from sqlalchemy.ext.associationproxy #2305

Closed gikpro closed 1 year ago

gikpro commented 1 year ago

My flask app crashes after packages update. In my log this code below causing the crash. Can you fix it ? https://github.com/flask-admin/flask-admin/blob/5a525d03de2e92fa124f7f9229dfde9dba50aef8/flask_admin/contrib/sqla/tools.py#L11

I am using the version 2 beta of SQLAlchemy.

BenjaminLECHA commented 1 year ago

The example : examples/sqla-association_proxyrun with the latest version of each dependancies :

SQLAlchemy : 2.0.0b3 Flask-SQLAlchely : 3.0.2 Flask-Admin : 1.6.0

Could you add more information ? (dependancies version, error message, ...)

gikpro commented 1 year ago

I think you may have mistyped the ASSOCIATION_PROXY, because the associationproxy.py contains only AssociationProxy class and association_proxy function.

I think that you must use one of the two in the place of this line in the .../contrib/sqla/tools.py module.

from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.associationproxy import AssociationProxy

The error is below

Traceback (most recent call last):
  File "/home/gikpro/PycharmProjects/PYAYSTREAM/run.py", line 8, in <module>
    app = create_app(DevConfig)
  File "/home/gikpro/PycharmProjects/PYAYSTREAM/webapp/__init__.py", line 59, in create_app
    from .admin import create_module as admin_create_module
  File "/home/gikpro/PycharmProjects/PYAYSTREAM/webapp/admin/__init__.py", line 7, in <module>
    from flask_admin.contrib.sqla import ModelView
  File "/home/gikpro/.local/share/virtualenvs/PYAYSTREAM-ZriFSaTY/lib/python3.8/site-packages/flask_admin/contrib/sqla/__init__.py", line 2, in <module>
    from .view import ModelView
  File "/home/gikpro/.local/share/virtualenvs/PYAYSTREAM-ZriFSaTY/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 "/home/gikpro/.local/share/virtualenvs/PYAYSTREAM-ZriFSaTY/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' (/home/gikpro/.local/share/virtualenvs/PYAYSTREAM-ZriFSaTY/lib/python3.8/site-packages/sqlalchemy/ext/associationproxy.py)

A snippet of my codes For the admin package, in init file

from webapp import db
from flask_admin import Admin
from .commands__utils import admin_cli
from flask_admin.contrib.sqla import ModelView
from ..iptv.channel.models import Chaine
from ..iptv.language.models import Langue
from ..iptv.url.models import Url
from ..iptv.country.models import Pays
from ..iptv.category.models import Categorie
from ..iptv.tvg.models import Tvg
from ..auth.models import User
from ..auth.models import Role

admin = Admin(template_mode="bootstrap4")

def create_module(app, **kwargs):
    admin.init_app(app)
    models_iptv = [Chaine, Categorie, Pays, Langue, Url, Tvg]
    for model in models_iptv:
        admin.add_view(ModelView(model, db.session, category="IPTV Tables"))

    models_users = [User, Role]
    for model in models_users:
        admin.add_view(ModelView(model, db.session, category="Users Tables"))

    # add commands cli to Flask
    app.cli.add_command(admin_cli)

A snippet of the run.py

import os
from webapp import create_app

if os.environ.get("FLASK_ENV") == "development":
    from config import DevConfig
    app = create_app(DevConfig)
    app.testing = True  
elif os.environ.get("FLASK_ENV") == "production":
    from config import ProdConfig
    app = create_app(ProdConfig)

if __name__ == "__main__":

    app.run()

The packages in my environment

aiofile==3.8.1
aioredis==2.0.1
aiosmtplib==2.0.0
alembic==1.9.1
aniso8601==9.0.1
anyio==3.6.2
apispec==6.0.2
asgiref==3.6.0
async-timeout==4.0.2
asyncio==3.4.3
attrs==22.2.0
Authomatic==1.0.0
Babel==2.11.0
bcrypt==4.0.1
black==22.10.0
blinker==1.5
Bootstrap-Flask==2.2.0
cachelib==0.9.0
caio==0.9.11
certifi==2022.12.7
cffi==1.15.1
chardet==5.1.0
charset-normalizer==2.1.1
click==8.1.3
cryptography==38.0.4
dnspython==2.3.0rc1
email-validator==1.3.0
flasgger==0.9.5
Flask==2.2.2
Flask-Admin==1.6.0
flask-apispec==0.11.4
Flask-Assets==2.0
Flask-Babel==2.0.0
Flask-BabelEx==0.9.4
Flask-Caching==2.0.1
Flask-Cors==3.0.10
Flask-JWT-Extended==4.4.4
Flask-Login==0.6.2
Flask-Mail==0.9.1
Flask-Mailing==0.2.0
flask-marshmallow==0.14.0
Flask-Migrate==4.0.0
Flask-Moment==1.0.5
flask-msearch==0.2.9.2
Flask-RESTful==0.3.9
Flask-Sitemap==0.4.0
Flask-SQLAlchemy==3.0.2
Flask-thumbnails==1.1.0
-e git+https://github.com/maxcountryman/flask-uploads.git@f66d7dc93e684fa0a3a4350a38e41ae00483a796#egg=Flask_Uploads
Flask-User==1.0.2.2
Flask-WTF==1.0.1
greenlet==2.0.1
gunicorn==20.1.0
h11==0.14.0
httpcore==0.16.3
httpx==0.23.1
idna==3.4
importlib-metadata==5.2.0
importlib-resources==5.10.1
itsdangerous==2.1.2
Jinja2==3.1.2
jsonschema==4.17.3
Mako==1.2.4
MarkupSafe==2.1.1
marshmallow==3.19.0
marshmallow-sqlalchemy==0.28.1
mistune==3.0.0rc4
mypy-extensions==0.4.3
packaging==22.0
passlib==1.7.4
pathspec==0.10.2
pkgutil_resolve_name==1.3.10
platformdirs==2.5.4
psycopg2==2.9.5
psycopg2-binary==2.9.5
pycparser==2.21
pydantic==1.10.2
PyJWT==2.6.0
pyparsing==3.0.9
pyrsistent==0.19.2
python-dateutil==2.8.2
python-editor==1.0.4
python-http-client==3.3.7
python-slugify==7.0.0
pytz==2022.7
PyYAML==6.0
requests==2.28.1
rfc3986==1.5.0
schedule==1.1.0
six==1.16.0
sniffio==1.3.0
speaklater==1.3
SQLAlchemy==2.0.0b4
starkbank-ecdsa==2.2.0
text-unidecode==1.3
tomli==2.0.1
typing_extensions==4.4.0
urllib3==1.26.13
webargs==8.2.0
webassets==2.0
Werkzeug==2.2.2
WTForms==3.0.1
zipp==3.11.0
alvarontwrk commented 1 year ago

Hi!

@gikpro Did you manage to fix your issue? I just got the same error :$

nixorn commented 1 year ago

Same problem. Because of that from flask_admin.contrib import sqla does not work.

cezar1 commented 1 year ago

Hello everyone.

Getting same error after updated the sqlalchemy to 2.0

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

  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)                                                                                               
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import                                                                                             
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load                                                                                          
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked                                                                                           
File "<frozen importlib._bootstrap_external>", line 883, in exec_module                                                                                     
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed

    from flask_admin.contrib.sqla import ModelView
  File "/home/.../venv/lib/python3.10/site-packages/flask_admin/contrib/sqla/__init__.py", line 2, in <module>
    from .view import ModelView
  File "/home/.../venv/lib/python3.10/site-packages/flask_admin/contrib/sqla/view.py", line 18, in <module>
    from flask_admin.contrib.sqla.tools import is_relationship
  File "/home/.../venv/lib/python3.10/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' (/home/.../venv/lib/python3.10/site-packages/sqlalchemy/ext/associationproxy.py)

PIP packages: SQLAlchemy==2.0.0 Flask==2.2.2 Flask-Admin==1.6.0 Flask-Login==0.6.2 flask-sock==0.6.0 Flask-SQLAlchemy==3.0.2

alvarontwrk commented 1 year ago

It looks like the last compatible version is SQLAlchemy 1.4.46 (the previous latest release).

SQLAlchemy 2.x was, until yesterday, pre-releases. So probably they were not fully tested in flask-admin.

If you run the example from this repo, you get the same error.

vokiatik commented 1 year ago

So how to workaround this iisue? Cause pip install flask_sqlalchemy crushes with this error. Is there any way to point which version of sqlalchemy should flask_sqlalchemy use?

alvarontwrk commented 1 year ago

@vokiatik you should be good specifying the version of the dependency:

pip install flask_sqlalchemy sqlalchemy==1.4.46

mrjoes commented 1 year ago

If someone wants to create a fix pull request - feel free to submit. Most likely the ASSOCIATION_PROXY was renamed and we need to support a new name.

mrjoes commented 1 year ago

I pushed the fix to master, can someone try it with SQLAlchemy 2.0?

alvarontwrk commented 1 year ago

Works great for me :)

Thanks!

Edit: ETA for release? Just for schedule myself :D

priamai commented 1 year ago

Waiting for a fix thanks!

nodebe commented 1 year ago

Just ran into this issue now. Wanted to know if a fix have been implemented

jessielw commented 1 year ago

Just ran into this issue now. Wanted to know if a fix have been implemented

It's fixed on the repository here, but there has not been an updated release yet.

You can use pip install git+(this repo url) to use it for now.

nodebe commented 1 year ago

Just ran into this issue now. Wanted to know if a fix have been implemented

It's fixed on the repository here, but there has not been an updated release yet.

You can use pip install git+(this repo name) to use it for now.

Thank you. I'll try that now

nodebe commented 1 year ago

Just ran into this issue now. Wanted to know if a fix have been implemented

It's fixed on the repository here, but there has not been an updated release yet.

You can use pip install git+(this repo name) to use it for now.

When I tried running: pip install git+flask-admin it gives me error ERROR: Invalid requirement: 'git+flask-admin' Maybe I'm not doing it right

jessielw commented 1 year ago

Just ran into this issue now. Wanted to know if a fix have been implemented

It's fixed on the repository here, but there has not been an updated release yet.

You can use pip install git+(this repo name) to use it for now.

When I tried running: pip install git+flask-admin it gives me error ERROR: Invalid requirement: 'git+flask-admin' Maybe I'm not doing it right

git+https://github.com/flask-admin/flask-admin

samialfattani commented 1 year ago

Just ran into this issue now. Wanted to know if a fix have been implemented

It's fixed on the repository here, but there has not been an updated release yet. You can use pip install git+(this repo name) to use it for now.

When I tried running: pip install git+flask-admin it gives me error ERROR: Invalid requirement: 'git+flask-admin' Maybe I'm not doing it right

git+https://github.com/flask-admin/flask-admin

Yes i tried it, and it is been fixed in the repo.

jessielw commented 1 year ago

1.4.46

Hey.. I tried to install from the git repository but it stills install the version 1.6.0.. :/ How to make it work from repository?

Really depends on how your program is installing stuff. In docker? Running on server?

The requirements.txt?

Uninstall the old version first, then reinstall it.

nodebe commented 1 year ago

Just ran into this issue now. Wanted to know if a fix have been implemented

It's fixed on the repository here, but there has not been an updated release yet. You can use pip install git+(this repo name) to use it for now.

When I tried running: pip install git+flask-admin it gives me error ERROR: Invalid requirement: 'git+flask-admin' Maybe I'm not doing it right

git+https://github.com/flask-admin/flask-admin

Thank you. This works.