@@ -505,7 +505,13 @@ class Timesheet(SQLModel, table=True):
505
505
# invoice: "Invoice" = Relationship(back_populates="timesheet")
506
506
# period: str
507
507
comment : Optional [str ] = Field (description = "A comment on the timesheet." )
508
- items : List [TimeTrackingItem ] = Relationship (back_populates = "timesheet" )
508
+ items : List [TimeTrackingItem ] = Relationship (
509
+ back_populates = "timesheet" ,
510
+ sa_relationship_kwargs = {
511
+ "lazy" : "subquery" ,
512
+ "cascade" : "all, delete" , # delete all items when deleting a timesheet
513
+ },
514
+ )
509
515
510
516
rendered : bool = Field (
511
517
default = False ,
@@ -572,7 +578,10 @@ class Invoice(SQLModel, table=True):
572
578
# Invoice 1:n Timesheet
573
579
timesheets : List [Timesheet ] = Relationship (
574
580
back_populates = "invoice" ,
575
- sa_relationship_kwargs = {"lazy" : "subquery" },
581
+ sa_relationship_kwargs = {
582
+ "lazy" : "subquery" ,
583
+ "cascade" : "all, delete" , # delete all timesheets when invoice is deleted
584
+ },
576
585
)
577
586
578
587
# status -- corresponds to InvoiceStatus enum above
@@ -586,7 +595,10 @@ class Invoice(SQLModel, table=True):
586
595
# invoice items
587
596
items : List ["InvoiceItem" ] = Relationship (
588
597
back_populates = "invoice" ,
589
- sa_relationship_kwargs = {"lazy" : "subquery" },
598
+ sa_relationship_kwargs = {
599
+ "lazy" : "subquery" ,
600
+ "cascade" : "all, delete" , # delete all invoice items when invoice is deleted
601
+ },
590
602
)
591
603
rendered : bool = Field (
592
604
default = False ,
0 commit comments