question-mark
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.

delete_instance with recursive=True only works with one level

See original GitHub issue

I have a table with foreign key to itself

class Post(BaseModel):
    reply_to: Post = ForeignKeyField(
        'self', null=True, default=None, backref='child_posts', on_delete='CASCADE')

Then in db if i have

id reply_to_id
1 nulll
2 1
3 2
4 3
5 4

If i delete_instance(True, True) record id=2, it deletes ID 2,3 only and leave ID 4,5. It means it doesn’t delete recursively (only works for 1 level).

Did i miss something or i have to deploy delete recursively by myself?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
coleifercommented, Jun 9, 2019

I remembered that the other - in fact, main - reason for not supporting this is that we don’t know how deep into the graph to go without resolving foreign-keys at every step.

So with a non-self-referential graph of foreign-keys, we just make sure to issue a query for each node in the graph (each foreign-key). With self-referential, we would have to issue n deletes where n is the maximum depth of any row’s relations. But we don’t know how far to go without resolving at each step. So peewee doesn’t do this, but you could very easily write it yourself.

Or just use the database. The database knows how to delete a self-referential collection of rows. That is why the ON DELETE constraint exists. Use it!

I prefer to do delete on code rather than depend on db.

Why? To my thinking that’s 100% backwards.

0reactions
dzptcommented, Jun 8, 2019

avoid cycles in the dependency graph.

Can we check the type of foreign key before deleting, it would avoid circular delete? I prefer to do delete on code rather than depend on db.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Querying — peewee 3.15.4 documentation
To delete a single model instance, you can use the Model.delete_instance() shortcut. delete_instance() will delete the given model instance and can optionally ...
Read more >
Access VM metadata - Compute Engine - Google Cloud
Project metadata propagates to all VMs within a project, while instance metadata only applies to a single VM. Directory listings: Some metadata entries...
Read more >
SQLAlchemy Documentation - Read the Docs
of ed, and indicate that we'd like only the first result in the full list of rows. A User instance is returned which...
Read more >
Diff - platform/test/framework - Google Git
-1,12 +0,0 @@ -# To set up client_secrets.json (once only) - -* Go to https://console.cloud.google.com/projectselector/apis/credentials/consent - and create ...
Read more >
Node.js v19.3.0 Documentation
In most cases, AsyncLocalStorage works without issues. In rare situations, the current store is lost in one of the asynchronous operations. If your...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found