Coverage for polar/models/product_media.py: 86%

20 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 12:42 +0000

1from typing import TYPE_CHECKING 

2from uuid import UUID 

3 

4from sqlalchemy import ForeignKey, Integer, UniqueConstraint, Uuid 

5from sqlalchemy.orm import Mapped, declared_attr, mapped_column, relationship 

6 

7from polar.kit.db.models import RecordModel 

8 

9if TYPE_CHECKING: 9 ↛ 10line 9 didn't jump to line 10 because the condition on line 9 was never true

10 from polar.models import Product 

11 from polar.models.file import ProductMediaFile 

12 

13 

14class ProductMedia(RecordModel): 

15 __tablename__ = "product_medias" 

16 __table_args__ = (UniqueConstraint("product_id", "order"),) 

17 

18 product_id: Mapped[UUID] = mapped_column( 

19 Uuid, 

20 ForeignKey("products.id", ondelete="cascade"), 

21 primary_key=True, 

22 ) 

23 file_id: Mapped[UUID] = mapped_column( 

24 Uuid, 

25 ForeignKey("files.id", ondelete="cascade"), 

26 primary_key=True, 

27 ) 

28 order: Mapped[int] = mapped_column(Integer, index=True, nullable=False) 

29 

30 @declared_attr 

31 def product(cls) -> Mapped["Product"]: 

32 return relationship("Product", lazy="raise", back_populates="product_medias") 

33 

34 @declared_attr 

35 def file(cls) -> Mapped["ProductMediaFile"]: 

36 # This is an association table, so eager loading makes sense 

37 return relationship("ProductMediaFile", lazy="joined")