fix: 重构用户相关数据库模型和关系

- 更新了 User 类中的多个关系属性,修正了部分属性的拼写错误和关联关系
- 修改了 LazerUserProfileSections 类的关联关系
- 修正了 LazerUserBanners 类的结构和关联关系
- 更新了 create_sample_data.py 中的统计类引用
- 在 config.py 中更新了数据库连接 URL
This commit is contained in:
jimmy-sketch
2025-07-19 21:33:11 +08:00
parent 91f15d572d
commit 451ef2f1d2
3 changed files with 35 additions and 15 deletions

View File

@@ -5,7 +5,7 @@ load_dotenv()
class Settings:
# 数据库设置
DATABASE_URL: str = os.getenv("DATABASE_URL", "mysql+pymysql://root:password@localhost:3306/osu_api")
DATABASE_URL: str = os.getenv("DATABASE_URL", "mysql+pymysql://root:Chinabug610@localhost:3306/osu_api")
REDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379/0")
# JWT 设置

View File

@@ -62,14 +62,28 @@ class User(Base):
lazer_profile = relationship("LazerUserProfile", back_populates="user", uselist=False, cascade="all, delete-orphan")
lazer_statistics = relationship("LazerUserStatistics", back_populates="user", cascade="all, delete-orphan")
lazer_achievements = relationship("LazerUserAchievement", back_populates="user", cascade="all, delete-orphan")
lazer_profile_sections=relationship("LazerUserProfileSection", back_populates="user", cascade="all, delete-orphan")
lazer_profile_sections = relationship(
"LazerUserProfileSections", # 修正类名拼写添加s
back_populates="user",
cascade="all, delete-orphan"
)
statistics = relationship("LegacyUserStatistics", back_populates="user", cascade="all, delete-orphan")
achievements = relationship("LazerUserAchievement", back_populates="user", cascade="all, delete-orphan")
achievements = relationship(
"LazerUserAchievement",
back_populates="user",
cascade="all, delete-orphan",
overlaps="lazer_achievements"
)
team_membership = relationship("TeamMember", back_populates="user", cascade="all, delete-orphan")
daily_challenge_stats = relationship("DailyChallengeStats", back_populates="user", uselist=False, cascade="all, delete-orphan")
rank_history = relationship("RankHistory", back_populates="user", cascade="all, delete-orphan")
avatar = relationship("UserAvatar", back_populates="user", primaryjoin="and_(User.id==UserAvatar.user_id, UserAvatar.is_active==True)", uselist=False)
active_banners=relationship("LazerUserBanners",back_populates="user",cascade="all, delete-orphan")
active_banners = relationship(
"UserAvatar", # 原定义指向LazerUserBanners实际应为UserAvatar
back_populates="user",
primaryjoin="and_(User.id==UserAvatar.user_id, UserAvatar.is_active==True)",
uselist=False
)
lazer_badges = relationship("LazerUserBadge", back_populates="user", cascade="all, delete-orphan")
lazer_monthly_playcounts = relationship("LazerUserMonthlyPlaycounts", back_populates="user", cascade="all, delete-orphan")
lazer_previous_usernames = relationship("LazerUserPreviousUsername", back_populates="user", cascade="all, delete-orphan")
@@ -132,6 +146,7 @@ class LazerUserProfile(Base):
# 关联关系
user = relationship("User", back_populates="lazer_profile")
class LazerUserProfileSections(Base):
__tablename__ = "lazer_user_profile_sections"
@@ -143,7 +158,8 @@ class LazerUserProfileSections(Base):
created_at=Column(DateTime, default=datetime.utcnow)
updated_at=Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
user = relationship("User", back_populates="lazer_profile_order")
user = relationship("User", back_populates="lazer_profile_sections")
class LazerUserCountry(Base):
__tablename__ = "lazer_user_countries"
@@ -251,13 +267,17 @@ class LazerUserStatistics(Base):
class LazerUserBanners(Base):
__tablename__ = "lazer_user_tournament_banners"
id=Column(Integer,primary_key=True)
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
tournament_id=Column(Integer, nullable=False)
image_url = Column(VARCHAR(500),nullable=False)
is_active=Column(TINYINT(1))
tournament_id = Column(Integer, nullable=False)
image_url = Column(VARCHAR(500), nullable=False)
is_active = Column(TINYINT(1))
user=relationship("User", back_populates="lazer_active_banners")
# 修正user关系的back_populates值
user = relationship(
"User",
back_populates="active_banners" # 改为实际存在的属性名
)
class LazerUserAchievement(Base):

View File

@@ -7,7 +7,7 @@ from datetime import datetime, timedelta
import time
from sqlalchemy.orm import Session
from app.dependencies import get_db, engine
from app.database import Base, User, UserStatistics, UserAchievement, DailyChallengeStats, RankHistory
from app.database import Base, User, LazerUserStatistics, UserAchievement, DailyChallengeStats, RankHistory
from app.auth import get_password_hash
# 创建所有表
@@ -134,7 +134,7 @@ def create_sample_user():
db.refresh(user)
# 创建 osu! 模式统计
osu_stats = UserStatistics(
osu_stats = LazerUserStatistics(
user_id=user.id,
mode="osu",
count_100=276274,
@@ -165,7 +165,7 @@ def create_sample_user():
)
# 创建 taiko 模式统计
taiko_stats = UserStatistics(
taiko_stats = LazerUserStatistics(
user_id=user.id,
mode="taiko",
count_100=160,
@@ -188,7 +188,7 @@ def create_sample_user():
)
# 创建 fruits 模式统计
fruits_stats = UserStatistics(
fruits_stats = LazerUserStatistics(
user_id=user.id,
mode="fruits",
count_100=109,
@@ -212,7 +212,7 @@ def create_sample_user():
)
# 创建 mania 模式统计
mania_stats = UserStatistics(
mania_stats = LazerUserStatistics(
user_id=user.id,
mode="mania",
count_100=7867,