从RDS检索数据会导致AttributeError:'sqlalchemy.cimmutabledict.immutabledict'对象没有属性'setdefault' [英] Retrieving data from RDS gives AttributeError: 'sqlalchemy.cimmutabledict.immutabledict' object has no attribute 'setdefault'

查看:121
本文介绍了从RDS检索数据会导致AttributeError:'sqlalchemy.cimmutabledict.immutabledict'对象没有属性'setdefault'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当调用API端点 {{url}}/api/users/login 进行用户身份验证时,我都希望从RDS检索用户数据,但是,我目前在检索数据时遇到问题来自RDS.

I would like to retrieve user data from RDS whenever the API endpoint {{url}}/api/users/login is called to authenticate users, however, I am currently having issues with retrieving data from RDS.

这是错误的完整堆栈跟踪:

This is the full stacktrace of the error:

Flask==1.1.2
Flask-Cors==3.0.10
flask-marshmallow==0.14.0
Flask-Migrate==2.5.3
Flask-RESTful==0.3.8
Flask-SQLAlchemy==2.4.4
marshmallow==3.10.0
marshmallow-sqlalchemy==0.24.1
SQLAlchemy==1.4.0b1
python==3.7

我解决问题的尝试

我尝试解决该问题但无济于事的一些事情是:

My attempts to solve the problem

Some things that I've attempted to resolve the issue but to no avail are:

  • 使用pymysql连接器代替mysqlconnector
  • 降级flask-marshmallow == 0.11.0
  • 降级的棉花糖== 3.6.1
  • 降级的棉花糖-sqlalchemy == 0.23.1

我已经对我的应用程序进行了Docker化,这是我当前的设置:

I have Dockerized my application and this is my current set up:

app.py

from flask_marshmallow import Marshmallow
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager

db = SQLAlchemy()
ma = Marshmallow()
migrate = Migrate()

def register_extensions(app: Flask) -> None:
   db.init_app(app)
   migrate.init_app(app, db)
   ma.init_app(app)

def create_application() -> Flask:
   app: Flask = Flask(__name__)
   app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqlconnector://{DB_USER}:{DB_PASSWORD}@{DB_INSTANCE}:{DB_PORT}"
   app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
   register_extensions(app)
   app.register_blueprint(users_urls.mod, url_prefix=f"/{BEP_PREFIX}/users")

   return app

 app = create_application()
 CORS(app)

models.py

class Accounts(db.Model):
    __tablename__ = 'Accounts'
    username = db.Column(db.String(100), primary_key=True)
    password = db.Column(db.String(256), nullable=False)
    email = db.Column(db.String(255), nullable=False)
    personnelID = db.Column(db.String(255), nullable=False)
    userType = db.Column(db.String(45), nullable=False)

service.py

class UserService(Service):
    __model__ = Accounts

    def get(self, username):
        return Accounts.query.filter_by(username=username).first()

user_service = UserService()

views.py

from bep.users.services import user_service

class UserLogin(Resource):
   def post(self):
       data = request.get_json()

       try:
           userData = user_service.get("test")
       except Exception:
           error_str = traceback.format_exc()
           logger.error("This prints the stack", exc_info=True)
           return UserServiceError.to_response(error=error_str, comments="Something went wrong while logging in") 

推荐答案

问题是,是由SQLAchemy 1.4的更改引起的.Flask-sqlalchemy尝试修改SQLALchemy引擎的URL,但是这些URL在SQLAlchemy 1.4中是不可变的.

The problem is a known issue in flask-sqlalchemy, caused by changes in SQLAchemy 1.4. Flask-sqlalchemy attempts to modify the SQLALchemy engine's URL, but these URLs are immutable in SQLAlchemy 1.4.

该问题已在Flask-SQLAlchemy 2.5+中得以解决(

The problem is fixed in Flask-SQLAlchemy 2.5+ (changelog).

如果无法升级Flask-SQLAlchemy,则可以通过以下方式解决此问题:通过命令行指定传递给 pip 的SQLAlchemy版本

If upgrading Flask-SQLAlchemy is not possible, the issue can be worked around by specifying the SQLAlchemy version passed to pip, either via the command line

pip install --upgrade 'SQLAlchemy<1.4'

或在requirements.txt

or in requirements.txt

SQLAlchemy<1.4

SQLAlchemy 1.4于2021年3月15日正式发布.

SQLAlchemy 1.4 went on general release on 15 March 2021.

这篇关于从RDS检索数据会导致AttributeError:'sqlalchemy.cimmutabledict.immutabledict'对象没有属性'setdefault'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆