Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
42 lines (33 loc) · 1.56 KB
/
Copy pathmodels.py
File metadata and controls
42 lines (33 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from database import Base
from datetime import datetime
class User(Base):
"""
User model - represents users who can create tasks
"""
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, nullable=False, index=True)
email = Column(String, unique=True, nullable=False, index=True)
hashed_password = Column(String, nullable=False) # NEVER store plain passwords!
created_at = Column(DateTime, default=datetime.utcnow)
# Relationship: One user has many tasks
# When you access user.tasks, you get all tasks belonging to this user
tasks = relationship("Task", back_populates="owner", cascade="all, delete-orphan")
class Task(Base):
"""
Task model - represents tasks owned by users
"""
__tablename__ = "tasks"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, nullable=False)
description = Column(String, nullable=True)
status = Column(String, default="todo") # todo, in_progress, done
priority = Column(String, default="medium") # low, medium, high
created_at = Column(DateTime, default=datetime.utcnow)
# Foreign Key: Links task to user
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
# Relationship: Each task belongs to one user
# When you access task.owner, you get the user who owns this task
owner = relationship("User", back_populates="tasks")