# Using WITH and an sql request breaks undo functionaity

**URL:** <https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352>\
**Category:** Development\
**Created:** [July 9, 2026, 12:17pm UTC](https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352 "2026-07-09T12:17:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eltaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/eltaurus/32/10287_2.png) [@Eltaurus](https://forums.ankiweb.net/u/Eltaurus)\
**Post date:** [July 9, 2026, 12:17pm UTC](https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352/1 "2026-07-09T12:17:24Z")

</div>

Steps to reproduce:

1. Review a card from a deck with multiple cards due

2. In the debug console, execute the following request:

3. Press `undo` in the reviewer (in won’t have any effect):

Going through the same steps, but shortening the request to just “SELECT ease FROM revlog” does not trigger the issue (the previous card rating gets undone as expected).

* * *

Calling the same request with an add-on

> **minimal \_\_init\_\_.py code:**
>
> ```python
> from aqt import mw, gui_hooks
> 
> def break_undo(*args, **kwargs):
> mw.col.db.all("WITH e AS (SELECT ease FROM revlog) SELECT ease FROM revlog")
> 
> gui_hooks.reviewer_did_show_question.append(break_undo)
> 
> ```

makes the undo button permanently grayed out during reviews:

![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/3X/6/a/6a8828e3c2641d81d1a2bee823fbb3ce8d6167f0.png)

Just as with a standalone command, removing the “WITH …” part from the query string prevents the issue from appearing

* * *

Tested in 26.05 (e64c6b1a)⁩ on win 11 (26200.8655) with all add-ons turned off

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [July 9, 2026, 1:16pm UTC](https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352/2 "2026-07-09T13:16:51Z")

</div>

The backend has a very simple check for mutating SQL commands: [anki/rslib/src/backend/dbproxy.rs at f6ac1985ba8c05405289b5a4454767de12208e31 · ankitects/anki · GitHub](https://github.com/ankitects/anki/blob/f6ac1985ba8c05405289b5a4454767de12208e31/rslib/src/backend/dbproxy.rs#L170)

Any command not starting with “select” is considered mutating and clears the undo/study queues for safety.

---

<div class="post-metadata">

**Author:** ![Eltaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/eltaurus/32/10287_2.png) [@Eltaurus](https://forums.ankiweb.net/u/Eltaurus)\
**Post date:** [July 9, 2026, 2:30pm UTC](https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352/3 "2026-07-09T14:30:56Z")

</div>

In that case, the workaround is to wrap anything else in a dummy `SELECT * FROM (...)`, I suppose

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [July 9, 2026, 3:52pm UTC](https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352/4 "2026-07-09T15:52:37Z")

</div>

Or avoiding direct DB writes if possible. We can add a new method to Anki if something is not already covered by the API.

---

<div class="post-metadata">

**Author:** ![Eltaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/eltaurus/32/10287_2.png) [@Eltaurus](https://forums.ankiweb.net/u/Eltaurus)\
**Post date:** [July 9, 2026, 3:56pm UTC](https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352/5 "2026-07-09T15:56:55Z")

</div>

I wasn’t trying to write anything, just querying revlog for detailed stats

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [August 8, 2026, 3:57pm UTC](https://forums.ankiweb.net/t/using-with-and-an-sql-request-breaks-undo-functionaity/70352/6 "2026-08-08T15:57:44Z")

</div>

Closed on August 8, 2026, 3:57pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
