File size: 2,143 Bytes
1f70d93 | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | """
SQLAlchemy ORM model for User entity.
This module defines the database schema for the users table.
Handles user authentication credentials and OAuth linking.
"""
from datetime import datetime
from typing import Optional
from sqlalchemy import DateTime, Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from core.database import Base
class User(Base):
"""
SQLAlchemy ORM model for User entity.
Represents the users table in PostgreSQL database.
Stores authentication credentials and OAuth account links.
Attributes:
id: Primary key, auto-incrementing integer
email: Unique email address (required, indexed)
password_hash: bcrypt-hashed password (required)
google_id: Google account ID for OAuth linking (optional)
created_at: Timestamp when user was created
updated_at: Timestamp when user was last modified
"""
__tablename__ = "users"
# Primary key with auto-increment
id: Mapped[int] = mapped_column(
Integer,
primary_key=True,
index=True,
autoincrement=True
)
# Required field: unique email address
email: Mapped[str] = mapped_column(
String(255),
unique=True,
nullable=False,
index=True
)
# Required field: bcrypt-hashed password
password_hash: Mapped[str] = mapped_column(
String(255),
nullable=False
)
# Optional field: Google OAuth account ID
google_id: Mapped[Optional[str]] = mapped_column(
String(255),
unique=True,
nullable=True,
index=True
)
# Automatic timestamp on creation
created_at: Mapped[datetime] = mapped_column(
DateTime,
default=datetime.utcnow,
nullable=False
)
# Automatic timestamp on create and update
updated_at: Mapped[datetime] = mapped_column(
DateTime,
default=datetime.utcnow,
onupdate=datetime.utcnow,
nullable=False
)
def __repr__(self) -> str:
"""String representation for debugging"""
return f"<User(id={self.id}, email='{self.email}')>"
|