Kamixitong/debug_database_config.py
2025-11-11 23:04:01 +08:00

47 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# 在导入应用之前加载环境变量
try:
from dotenv import load_dotenv
if load_dotenv():
print("成功加载.env文件")
else:
print("未找到或无法加载.env文件")
except ImportError:
print("python-dotenv未安装跳过.env文件加载")
print("Environment variables:")
print(f"DATABASE_URL: {os.environ.get('DATABASE_URL', 'Not set')}")
from app import create_app, db
# 创建应用实例
app = create_app()
print("\nApp config:")
print(f"SQLALCHEMY_DATABASE_URI: {app.config.get('SQLALCHEMY_DATABASE_URI')}")
with app.app_context():
print("\nDatabase engine info:")
print(f"Engine URL: {db.engine.url}")
try:
# 测试数据库连接
connection = db.engine.connect()
print("✓ Database connection successful")
connection.close()
# 检查表结构
from sqlalchemy import inspect
inspector = inspect(db.engine)
tables = inspector.get_table_names()
print(f"Database tables: {tables}")
except Exception as e:
print(f"✗ Database connection failed: {e}")
import traceback
traceback.print_exc()