Browse Source

fix: remove .query reference of db.Model

Yeuoly 6 months ago
parent
commit
53e1b45d40
1 changed files with 9 additions and 1 deletions
  1. 9 1
      api/controllers/console/apikey.py

+ 9 - 1
api/controllers/console/apikey.py

@@ -1,6 +1,8 @@
 import flask_restful
 from flask_login import current_user
 from flask_restful import Resource, fields, marshal_with
+from sqlalchemy import select
+from sqlalchemy.orm import Session
 from werkzeug.exceptions import Forbidden
 
 from extensions.ext_database import db
@@ -25,7 +27,13 @@ api_key_list = {"data": fields.List(fields.Nested(api_key_fields), attribute="it
 
 
 def _get_resource(resource_id, tenant_id, resource_model):
-    resource = resource_model.query.filter_by(id=resource_id, tenant_id=tenant_id).first()
+    if resource_model == App:
+        with Session(db.engine) as session:
+            resource = session.execute(
+                select(resource_model).filter_by(id=resource_id, tenant_id=tenant_id)
+            ).scalar_one_or_none()
+    else:
+        resource = resource_model.query.filter_by(id=resource_id, tenant_id=tenant_id).first()
 
     if resource is None:
         flask_restful.abort(404, message=f"{resource_model.__name__} not found.")