76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""Portfolio model for WealthWise.
|
|
|
|
This module defines the Portfolio database model. Portfolios belong to users
|
|
and contain multiple transactions. Users can have multiple portfolios for
|
|
different investment strategies or purposes.
|
|
"""
|
|
|
|
from typing import TYPE_CHECKING, List, Optional
|
|
from uuid import UUID
|
|
|
|
from sqlalchemy import Column, ForeignKey, String, Text
|
|
from sqlmodel import Field, Relationship, SQLModel
|
|
|
|
from app.models.base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.transaction import Transaction
|
|
from app.models.user import User
|
|
|
|
|
|
class PortfolioBase(SQLModel):
|
|
"""Base Portfolio model with common attributes.
|
|
|
|
Attributes:
|
|
name: Portfolio name
|
|
description: Optional portfolio description
|
|
"""
|
|
|
|
name: str = Field(
|
|
sa_column=Column(String(255), nullable=False),
|
|
description="Portfolio name",
|
|
)
|
|
description: Optional[str] = Field(
|
|
default=None,
|
|
sa_column=Column(Text, nullable=True),
|
|
description="Optional portfolio description",
|
|
)
|
|
|
|
|
|
class Portfolio(BaseModel, PortfolioBase, table=True):
|
|
"""Portfolio database model.
|
|
|
|
Portfolios represent collections of financial transactions owned by a user.
|
|
Each portfolio tracks investments, expenses, or savings for a specific purpose.
|
|
|
|
Attributes:
|
|
id: UUID primary key (inherited from BaseModel)
|
|
user_id: Foreign key to users table
|
|
name: Portfolio name
|
|
description: Optional description
|
|
user: Relationship to owner
|
|
transactions: Relationship to portfolio transactions
|
|
created_at: Timestamp (inherited from BaseModel)
|
|
updated_at: Timestamp (inherited from BaseModel)
|
|
"""
|
|
|
|
__tablename__ = "portfolios"
|
|
|
|
user_id: UUID = Field(
|
|
sa_column=Column(
|
|
ForeignKey("users.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
),
|
|
description="Owner user ID",
|
|
)
|
|
|
|
# Relationships
|
|
user: "User" = Relationship(back_populates="portfolios")
|
|
transactions: List["Transaction"] = Relationship(
|
|
back_populates="portfolio",
|
|
sa_relationship_kwargs={"cascade": "all, delete-orphan"},
|
|
)
|
|
|
|
class Config:
|
|
"""Pydantic configuration."""
|
|
arbitrary_types_allowed = True |