Skip to content

Commit 1087fac

Browse files
committed
docs: add-warning-about-backslash-corruption-when-building-CQL-with-string-formatting
1 parent 6fbcacb commit 1087fac

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

docs/getting_started.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,29 @@ Althought it is not recommended, you can also pass parameters to non-prepared
215215
statements. The driver supports two forms of parameter place-holders: positional
216216
and named.
217217

218+
.. warning::
219+
220+
**Avoid "Round-Tripping" data using string formatting.**
221+
222+
Never use f-strings or ``%`` to insert data — especially driver-returned collections (maps, sets, lists) — back into a CQL query.
223+
224+
**The Gotcha:** The driver's collection objects use Python's ``repr()`` formatting for nested values.
225+
This automatically doubles backslashes (e.g., ``\`` becomes ``\\``).
226+
Because CQL does not use backslashes as escape characters,
227+
Cassandra will store those extra backslashes literally, **corrupting your data**.
228+
229+
**The Fix:** Always use **prepared statements**.
230+
They transmit data in a binary format that bypasses Python's string serialization entirely.
231+
232+
.. code-block:: python
233+
234+
# BAD: f-strings cause double-escaping/corruption
235+
session.execute(f"UPDATE t SET my_map={row.my_map} WHERE id=1")
236+
237+
# GOOD: Prepared statements preserve data exactly
238+
stmt = session.prepare("UPDATE t SET my_map=? WHERE id=1")
239+
session.execute(stmt, [row.my_map])
240+
218241
Positional parameters are used with a ``%s`` placeholder. For example,
219242
when you execute:
220243

0 commit comments

Comments
 (0)