Cascade Delete Issue
See original GitHub issueI can’t seem to get cascade deletes working with FKs on a postgres DB. Should it work by default?
I tried adding the ondelete=“CASCADE” kwarg to my FK relations, but nothing seemed to happen on the other side of the model.
In short, I have:
class Project(BaseModel):
class Meta(ormar.ModelMeta):
tablename = "projects"
name: str = ormar.String(max_length=256)
# <SNIP>
class Quote(BaseModel):
class Meta(ormar.ModelMeta):
tablename = "quotes"
# <SNIP>
project: Project = ormar.ForeignKey(
Project, related_name="quotes", ondelete="CASCADE"
)
And the routing endpoint I have in FastAPI:
@router.delete("/{project_id}")
async def delete_project(request: Request, project_id: uuid.UUID):
project = await Project.objects.filter(client=request.state.client).get(
id=project_id
)
await project.delete()
return {"deleted": True, "id": project.id}
But this seems to orphan the affected quote that was attached to the project. This looks to be a known caveat to SQLAlchemy when using filter().delete(). Is there an easy way to build in the same fixes? https://stackoverflow.com/questions/5033547/sqlalchemy-cascade-delete https://stackoverflow.com/questions/19243964/sqlalchemy-delete-doesnt-cascade
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
SQL Server: Foreign Keys with cascade delete - TechOnTheNet
A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the...
Read more >DELETE CASCADE and UPDATE CASCADE in SQL Server ...
In this article, we will review on DELETE CASCADE AND UPDATE CASCADE rules in SQL Server foreign key with different examples.
Read more >Good explanation of cascade (ON DELETE/UPDATE) behavior
ON DELETE CASCADE is an optional clause in a foreign key declaration. So it goes with the foreign key declaration. (Meaning, in the...
Read more >Using the ON DELETE CASCADE Option - IBM
Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in...
Read more >Cascade Delete - EF Core | Microsoft Learn
Cascading deletes are needed when a dependent/child entity can no longer be associated with its current principal/parent.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in 0.9.0 by #92
Yeah, but that how it’s supposed to look like out of the box 😃
I found the bug it’s related to deep-cloning sqlalchemy.Columns, will fix this and release new version soon.
Also will add names to foreign_keys so alembic can clearly identify constraints and not recreate them during consecutive runs.