Coverage for polar/models/product_benefit.py: 90%

19 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 Benefit, Product 

11 

12 

13class ProductBenefit(RecordModel): 

14 __tablename__ = "product_benefits" 

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

16 

17 product_id: Mapped[UUID] = mapped_column( 

18 Uuid, 

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

20 primary_key=True, 

21 ) 

22 benefit_id: Mapped[UUID] = mapped_column( 

23 Uuid, 

24 ForeignKey("benefits.id", ondelete="cascade"), 

25 primary_key=True, 

26 ) 

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

28 

29 @declared_attr 

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

31 return relationship("Product", lazy="raise", back_populates="product_benefits") 

32 

33 @declared_attr 

34 def benefit(cls) -> Mapped["Benefit"]: 

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

36 return relationship("Benefit", lazy="joined")