I'm a beginner of flask,when i tried to run app.py on the server,it seemed to be wrong.And it can works on localhost. I think it's wrong with the database and have tried to change the ip of database,but it not worked.
Traceback (most recent call last):
File "app.py", line 27, in <module>
import auth
File "/tmp/pycharm_project_flaskr/auth.py", line 5, in <module>
from db import get_db, User
File "/tmp/pycharm_project_flaskr/db.py", line 22, in <module>
db.init_app(app)
File "/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/extension.py", line 305, in init_app
engines = self._app_engines.setdefault(app, {})
File "/usr/local/lib/python3.7/weakref.py", line 489, in setdefault
return self.data.setdefault(ref(key, self._remove),default)
TypeError: cannot create weak reference to 'LocalProxy' object
That's the way about databases.
app = current_app
DB_URI = 'mysql pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy()
db.init_app(app)
CodePudding user response:
I change the version of flask from 2.2.2 to 2.0.3 and the problem was solved.
CodePudding user response:
I had the same issue yesterday (on Flask 2.2.2) only connecting to my production database, not local. I fixed it without downgrading Flask by re-ordering some of the steps initializing my app.
My final version was:
from flask import Flask
import os
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
with app.app_context():
database_uri = (
f'postgresql psycopg2://{os.getenv("POSTGRES_USER")}:'
f'{os.getenv("POSTGRES_PW")}@'
f'{os.getenv("POSTGRES_URL")}/'
f'{os.getenv("POSTGRES_DB")}'
)
app.config["SQLALCHEMY_DATABASE_URI"] = database_uri
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
db.create_all()
db.session.commit()
import api
api.add_all_apis(app=app)
if __name__ == "__main__":
app.run(host="0.0.0.0")