Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - Retrieving data from RDS gives AttributeError: 'sqlalchemy.cimmutabledict.immutabledict' object has no attribute 'setdefault'

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.

Error stacktrace

This is the full stacktrace of the error: enter image description here

Current package versions

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:

  • using pymysql connector instead of mysqlconnector
  • downgrade flask-marshmallow==0.11.0
  • downgraded marshmallow==3.6.1
  • downgraded marshmallow-sqlalchemy==0.23.1

More info about my application set up

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") 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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'

or in requirements.txt

SQLAlchemy<1.4

SQLAlchemy 1.4 went on general release on 15 March 2021.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...