why am I getting this error : AttributeError: 'LocalStack' object has no attribute '__ident_func__' in SQLAlchemy

Viewed 3786

I am trying to create an app with flask, and I am in this step that I need to connect my database which is in my local server, to achieve this I have used the command db = SQLAlchemy(app) and imported SQLALchemy from flask-alchemy(I have provided my codes bellow in detail).when I start my flask app server I get the error AttributeError: 'LocalStack' object has no attribute '--ident_func--' pointing to the line with db = SQLAlchemy(app). Any help will be appreciated cause I really don't know what I am missing.

app.py

#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy


#----------------------------------------------------------------------------#
# Configs
#----------------------------------------------------------------------------#
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

#----------------------------------------------------------------------------#
# Models
#----------------------------------------------------------------------------#
#to include my database schema models later

#----------------------------------------------------------------------------#
# routes
#----------------------------------------------------------------------------#
@app.route('/')
def index():
    return render_template('pages/home.html')


if __name__=='__main__':
    app.run()

config.py

# Enable debug mode.
DEBUG = True

# Connect to the database
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres@localhost:5432/fyyurdb'

home.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <h1>hello world</h1>
    </body>
</html>

pip freeze (requirements.txt)

Babel==2.9.0
click==8.1.3
colorama==0.4.4
Flask==2.1.2
Flask-Moment==0.11.0
Flask-SQLAlchemy==2.4.4
Flask-WTF==0.14.3
greenlet==1.1.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
python-dateutil==2.6.0
pytz==2022.1
six==1.16.0
SQLAlchemy==1.4.36
Werkzeug==2.1.2
WTForms==3.0.1

error massage on terminal

(fyyurenv) C:\Users\Gm Kahosh\Desktop\fyyur\cd0046-SQL-and-Data-Modeling-for-the-Web>python app.py
Traceback (most recent call last):
  File "C:\Users\Gm Kahosh\Desktop\fyyur\cd0046-SQL-and-Data-Modeling-for-the-Web\app.py", line 13, in <module>
    db = SQLAlchemy(app)
  File "C:\Users\Gm Kahosh\Desktop\fyyur\fyyurenv\lib\site-packages\flask_sqlalchemy\__init__.py", line 714, in __init__
    self.session = self.create_scoped_session(session_options)
  File "C:\Users\Gm Kahosh\Desktop\fyyur\fyyurenv\lib\site-packages\flask_sqlalchemy\__init__.py", line 747, in create_scoped_session                                                                                                                                 on
    scopefunc = options.pop('scopefunc', _app_ctx_stack.__ident_func__)
AttributeError: 'LocalStack' object has no attribute '__ident_func__'
5 Answers

I had a similar issue, I uninstalled it using python -m pip uninstall flask-sqlalchemy and installed with python -m pip install flask-sqlalchemy . The issue was solved.

I was able to fix this problem by editing my requirements.txt to include the psycopg2-binary instead of psycopg2 itself.

Afterwards, I re-built the docker environment (docker-compose build) and re-up'd the docker environment (docker-compose up).

In case future readers have not done so yet, I also needed to deactivate the docker environment before the above process (docker-compose down).

The root cause of this issue is that a change to the Werkzeug package - which Flask depends on - required a change to how Flask constructs scoped sessions. The change is documented in this change. The error occurs if the Werkzeug package has it's change but the Flask package does not.

Upgrading to Flask-SQLAlchemy >= 2.5.1 should solve the issue.

I really should have mentioned that the source codes I had cloned from a GitHub repo so there was code pollution that affected my code files I guess despite installing all the requirements with the command pip install -r requirement.txt. A work around this is that I deleted all my project files and started my project independently without any starter templates from the GitHub repo. This project was just a practice exercise for me but the big question which now remains is how to clone app and successfully install all these dependency without getting the error.

I had a similar problem. The reason you are experiencing the above issue is because you don't have psycopg2 installed on you machine. If you are working from a virtual environment add psycopg2 to your requirements.txt file then install it via the command pip install -r requirements.txt

Also, confirm that the connection to your local database is working.

Related