release: Elysia ToDo v1.0.0
鍏ㄦ爤涓汉淇℃伅绠$悊搴旂敤锛岄泦鎴愬緟鍔炰换鍔°€佷範鎯墦鍗°€佺邯蹇垫棩鎻愰啋銆佽祫浜ф€昏鍔熻兘銆 Made-with: Cursor
This commit is contained in:
55
api/app/models/habit.py
Normal file
55
api/app/models/habit.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from sqlalchemy import Column, Integer, String, Text, Boolean, Date, DateTime, ForeignKey, UniqueConstraint
|
||||
from sqlalchemy.orm import relationship
|
||||
from app.database import Base
|
||||
from app.utils.datetime import utcnow
|
||||
|
||||
|
||||
class HabitGroup(Base):
|
||||
"""习惯分组模型"""
|
||||
__tablename__ = "habit_groups"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String(100), nullable=False)
|
||||
color = Column(String(20), default="#FFB7C5")
|
||||
icon = Column(String(50), default="flag")
|
||||
sort_order = Column(Integer, default=0)
|
||||
|
||||
# 关联关系
|
||||
habits = relationship("Habit", back_populates="group", order_by="Habit.created_at")
|
||||
|
||||
|
||||
class Habit(Base):
|
||||
"""习惯模型"""
|
||||
__tablename__ = "habits"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String(200), nullable=False)
|
||||
description = Column(Text, nullable=True)
|
||||
group_id = Column(Integer, ForeignKey("habit_groups.id"), nullable=True)
|
||||
target_count = Column(Integer, default=1)
|
||||
frequency = Column(String(20), default="daily") # daily / weekly
|
||||
active_days = Column(String(100), nullable=True) # JSON 数组, 如 [0,2,4] 表示周一三五
|
||||
is_archived = Column(Boolean, default=False)
|
||||
created_at = Column(DateTime, default=utcnow)
|
||||
updated_at = Column(DateTime, default=utcnow, onupdate=utcnow)
|
||||
|
||||
# 关联关系
|
||||
group = relationship("HabitGroup", back_populates="habits")
|
||||
checkins = relationship("HabitCheckin", back_populates="habit", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class HabitCheckin(Base):
|
||||
"""习惯打卡记录模型"""
|
||||
__tablename__ = "habit_checkins"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("habit_id", "checkin_date", name="uq_habit_checkin_date"),
|
||||
)
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
habit_id = Column(Integer, ForeignKey("habits.id"), nullable=False)
|
||||
checkin_date = Column(Date, nullable=False)
|
||||
count = Column(Integer, default=0)
|
||||
created_at = Column(DateTime, default=utcnow)
|
||||
|
||||
# 关联关系
|
||||
habit = relationship("Habit", back_populates="checkins")
|
||||
Reference in New Issue
Block a user