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

20 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 17:15 +0000

1from typing import TYPE_CHECKING 1ab

2from uuid import UUID 1ab

3 

4from sqlalchemy import ForeignKey, Integer, UniqueConstraint, Uuid 1ab

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

6 

7from polar.kit.db.models import RecordModel 1ab

8 

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

10 from polar.models import Product 

11 from polar.models.file import ProductMediaFile 

12 

13 

14class ProductMedia(RecordModel): 1ab

15 __tablename__ = "product_medias" 1ab

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

17 

18 product_id: Mapped[UUID] = mapped_column( 1ab

19 Uuid, 

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

21 primary_key=True, 

22 ) 

23 file_id: Mapped[UUID] = mapped_column( 1ab

24 Uuid, 

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

26 primary_key=True, 

27 ) 

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

29 

30 @declared_attr 1ab

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

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

33 

34 @declared_attr 1ab

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

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

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