Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask_sqlalchemy: error with `__setattr__` to `DefaultMeta`

After re-building a Docker image, I've started to get the following error:

$ docker run --rm -it python:3.8-slim /bin/bash
...
$ pip install flask_sqlalchemy
...
$ python -c "from flask_sqlalchemy import SQLAlchemy; SQLAlchemy()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 715, in __init__
    self.Model = self.make_declarative_base(model_class, metadata)
  File "/usr/local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 797, in make_declarative_base
    model.query_class = self.Query
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/ext/declarative/api.py", line 79, in __setattr__
    _add_attribute(cls, key, value)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 802, in _add_attribute
    type.__setattr__(cls, key, value)
TypeError: can't apply this __setattr__ to DefaultMeta object

The versions of the specific packages involved:

python -c "import sqlalchemy, flask_sqlalchemy; print(sqlalchemy.__version__, flask_sqlalchemy.__version__)"
1.3.18 # sqlalchemy
2.4.3 # flask_sqlalchemy

Although I've also experienced the error with versions 1.3.17 and 2.4.1.

like image 905
David Wolever Avatar asked Jul 14 '20 13:07

David Wolever


People also ask

How to use SQLAlchemy with flask?

Step 1 - Install the Flask-SQLAlchemy extension. Step 2 - You need to import the SQLAlchemy class from this module. Step 3 - Now create a Flask application object and set the URI for the database to use.

How do I fix this error message in flask?

This error message indicates that the index.html template does not exist. To fix this, you’ll create a base.html template file other templates will inherit from to avoid code repetition, then an index.html template that extends the base template. Create the templates directory, which is the directory where Flask looks for template files.

Can I use SQL in a flask application?

Using raw SQL in the Flask Web application to perform CRUD operations on the database can be cumbersome. Instead, SQLAlchemy, the Python Toolkit is a powerful OR Mapper, which provides application developers with the full functionality and flexibility of SQL.

What is flask in Python?

Flask is a lightweight Python web framework that provides useful tools and features for creating web applications in the Python Language. It gives developers flexibility and is an accessible framework for new developers because you can build a web application quickly using only a single Python file.


1 Answers

After some experimentation, it seems like the issue is due to some change between python:3.8.3-slim and python:3.8.4-slim.

To reproduce, notice that this works:

$ docker run --rm -it python:3.8.3-slim …

Where this does not:

docker run --rm -it python:3.8.4-slim …

It looks like this bug/fix is the cause of the issue: https://bugs.python.org/issue39960

like image 102
David Wolever Avatar answered Oct 16 '22 15:10

David Wolever