Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cascade Delete Issue

See original GitHub issue

I 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:

async def delete_project(request: Request, project_id: uuid.UUID):
    project = await Project.objects.filter(client=request.state.client).get(
    await project.delete()
    return {"deleted": True, "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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

collerekcommented, Feb 2, 2021

Fixed in 0.9.0 by #92

collerekcommented, Feb 2, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found