zyc 79368c11e2
All checks were successful
Build and Deploy Backend / build-and-deploy (push) Successful in 1m25s
Build and Deploy Web / build-and-deploy (push) Successful in 56s
fix db bug
2026-02-14 10:01:20 +08:00

20 lines
605 B
Python
Raw Permalink 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.

"""数据库初始化"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from config import DATABASE_URL
# check_same_thread 仅 SQLite 需要
connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
engine = create_engine(DATABASE_URL, connect_args=connect_args)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
"""获取数据库会话FastAPI 依赖注入用)"""
db = SessionLocal()
try:
yield db
finally:
db.close()