django-simple-history
django-simple-history copied to clipboard
Updating an m2m set on a model with history causes a lot of duplicate queries
If I have the models e.g. User and Group where Group has an m2m field to User and also has a field like history = HistoricalRecords(m2m_fields=[users]), calling any of the m2m set update methods such as group.users.set(users) or group.users.add(user1, user2) causes duplicate queries.
I have an example of this where I add 25 users to a group, and it generates 25 extra user selects and 25 extra group selects.
Is there any way to work around this? Since I do this right after creating a new Group, I tried bypassing history saving on the Group model as suggested by the documentation, but related sets seem unaffected by this.
I'm not sure I'm able to reproduce your issue π€ Calling e.g. add() with multiple arguments should only create one historical record (see e.g. this test case)... Could you provide some (complete) test case code that reproduces your issue? π
This is not about duplicate records, but duplicate database queries, as identified by e.g. Django Debug Toolbar. Itβs a performance issue. Can you also see duplicate queries by inspecting with debug toolbar? If not, Iβll try and make a test case.
Ah, sorry, I misread your description π I'm able to reproduce it, and will open a PR for it :)
Thank you, lovely to hear! Let me know if I can help in any way.
Any update on this?
I've made a fix for it locally, but considering that @tim-schilling and I are the only currently active maintainers, I want to focus on finishing #1128 first, which seems close to being merged π
@alimony if this is an urgent thing for you, please write the test case and attempt a fix. Anything that gets things started will help tremendously.
Thanks a lot @ddabble !
@ddabble Any idea when we might see this in a release?
I'm planning on creating a new release after #1344 has been merged π
@alimony I've created a GitHub release for version 3.6.0, but it's yet to be published to PyPI; see progress in #1345