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__'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With