from datetime import datetime from sqlalchemy import DateTime, ForeignKey, Integer, String, func from sqlalchemy.orm import Mapped, mapped_column, relationship from .db.session import Base class User(Base): __tablename__ = 'users' id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) name: Mapped[str] = mapped_column(String(120), nullable=False) email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False) password_hash: Mapped[str] = mapped_column(String(255), nullable=False) last_login_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) addresses: Mapped[list['Address']] = relationship(back_populates='user', cascade='all, delete-orphan') class Address(Base): __tablename__ = 'addresses' id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) user_id: Mapped[int] = mapped_column(ForeignKey('users.id', ondelete='CASCADE'), index=True, nullable=False) house_number: Mapped[str] = mapped_column(String(50), nullable=False) colonia: Mapped[str] = mapped_column(String(120), nullable=False) street: Mapped[str] = mapped_column(String(160), nullable=False) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False) user: Mapped['User'] = relationship(back_populates='addresses')