-
        First Check
 Commit to Help
 Example Codefrom sqlmodel import Field, SQLModel, Relationship
from datetime import datetime
from typing import Optional, List
class GroupMembers(SQLModel, table=True):
    group_id: Optional[int] = Field(default=None, foreign_key="child.id", primary_key=True)
    member_id: Optional[int] = Field(default=None, foreign_key="member.id", primary_key=True)
    created_at: datetime = Field(default_factory=datetime.now)
    updated_at: datetime = Field(default_factory=datetime.now)
class Group(SQLModel, table=True):
    
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str = Field()
    created_at: datetime = Field(default_factory=datetime.now)
    updated_at: datetime = Field(default_factory=datetime.now)
   
    members: List["Member"] = Relationship(
        back_populates="groups",
        link_model=GroupMembers, 
        sa_relationship_kwargs=dict(lazy="subquery"),
    )
class Member(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    
    name: str = Field()
    
    created_at: datetime = Field(default_factory=datetime.now)
    updated_at: datetime = Field(default_factory=datetime.now)
   
    groups: List["Group"] = Relationship(
        back_populates="groups",
        link_model=GroupMembers,
    )
class MemberRead(SQLModel):
    id: int
    name: str
    
class GroupRead(SQLModel):
    id: int
    name: str
    members: Optional[List[MemberRead]]DescriptionI have a working FastAPI application where I had create / delete endpoints for members and groups above. I was adding read endpoints, and I want the  I achieved this by adding  However, after adding this, the delete functionality does not work - and error about foreign key violation occurs around the  Adding  What I want is 
 Has anyone come across this issue before where lazy loading appears to affect cascading deletes ? Operating SystemLinux, macOS Operating System DetailsNo response SQLModel Version0.0.6 Python Version3.9.11 Additional ContextNo response  | 
  
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
| 
         It turns out this was all caused by a delete of an object elsewhere in the code that referenced an instance of Group retrieved from a different session, which was revealed by adding this code, but caused by a different endpoint deleting incorrectly. Closing issue as resolved!  | 
  
Beta Was this translation helpful? Give feedback.
It turns out this was all caused by a delete of an object elsewhere in the code that referenced an instance of Group retrieved from a different session, which was revealed by adding this code, but caused by a different endpoint deleting incorrectly. Closing issue as resolved!