delete_instance with recursive=True only works with one level
See original GitHub issueI 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:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top 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 >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
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!Why? To my thinking that’s 100% backwards.
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.