Files

53 lines
2.1 KiB
Python

"""Address model — polymorphic addresses for companies and contacts."""
from __future__ import annotations
import uuid
from sqlalchemy import Boolean, Index, String, UniqueConstraint
from sqlalchemy.dialects.postgresql import UUID as PGUUID
from sqlalchemy.orm import Mapped, mapped_column
from app.core.db import Base, TenantMixin
class Address(Base, TenantMixin):
"""Polymorphic address entity — multiple addresses per company or contact.
entity_type: 'company' or 'contact'
entity_id: FK to companies.id or contacts.id
address_type: billing, shipping, headquarters, branch, private, other
is_default: one default address per (entity_type, entity_id, address_type)
"""
__tablename__ = "addresses"
__table_args__ = (
UniqueConstraint(
"tenant_id",
"entity_type",
"entity_id",
"address_type",
"is_default",
name="uq_address_default_per_type",
),
Index("ix_addresses_tenant_entity", "tenant_id", "entity_type", "entity_id"),
Index("ix_addresses_tenant_type", "tenant_id", "address_type"),
)
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
entity_type: Mapped[str] = mapped_column(String(50), nullable=False)
entity_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), nullable=False, index=True
)
label: Mapped[str] = mapped_column(String(100), nullable=False)
address_type: Mapped[str] = mapped_column(String(50), nullable=False)
street: Mapped[str | None] = mapped_column(String(255), nullable=True)
street_number: Mapped[str | None] = mapped_column(String(20), nullable=True)
city: Mapped[str | None] = mapped_column(String(100), nullable=True)
zip: Mapped[str | None] = mapped_column(String(20), nullable=True)
state: Mapped[str | None] = mapped_column(String(100), nullable=True)
country: Mapped[str | None] = mapped_column(String(2), nullable=True)
is_default: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)