fix: 改用information_schema查询有user_id列的表,彻底解决AsyncEngine/Session无法inspect的问题

This commit is contained in:
Pine
2026-09-06 20:58:08 +08:00
parent 3bd86b94ed
commit f4742ffbd1
+6 -16
View File
@@ -240,22 +240,12 @@ async def delete_user(
"market_purchase_orders", "market_purchases",
]
# 获取数据库中所有表的列信息,避免对没有 user_id 的表执行删除
from sqlalchemy import inspect as sa_inspect
def _get_tables_with_user_id(sync_conn):
insp = sa_inspect(sync_conn)
result = set()
for tname in cascade_tables:
try:
cols = [c["name"] for c in insp.get_columns(tname)]
if "user_id" in cols:
result.add(tname)
except Exception:
pass
return result
tables_with_user_id = await db.session.run_sync(_get_tables_with_user_id)
# 直接查 information_schema 获取有 user_id 列的表,避免对没有 user_id 的表执行删除
result = await db.session.execute(
sql_text("SELECT DISTINCT table_name FROM information_schema.columns WHERE column_name = 'user_id' AND table_schema = DATABASE()")
)
existing_user_id_tables = {row[0] for row in result.fetchall()}
tables_with_user_id = [t for t in cascade_tables if t in existing_user_id_tables]
deleted_count = 0
for table in tables_with_user_id: