crate / crate/sqlalchemy-cratedb
Refactor rewrite_update() for cleaner bind parameter key representation
- Dominant language
- Python
- Stars
- 9
- Forks
- 4
- Avg merge
- 6d 12h
- Merged PRs (30d)
- 3
Description
## Summary
The `rewrite_update()` function in `src/sqlalchemy_cratedb/compiler.py` constructs bind parameter keys for partial `ObjectType` updates using the following format:
```python
newparams["{0}['{1}']".format(key, subkey)] = subval
# e.g., key="data", subkey="x" → "data['x']"
```
SQLAlchemy's `IdentifierPreparer` then sanitizes bind param names by replacing `[` → `_` and `]` → `_`, but leaves the single quotes intact, resulting in keys like `data_'x'_`. This leaks internal bracket notation into the parameter dictionary, which is surprising for downstream consumers.
## Proposed Change
Change how parameter keys are constructed in `rewrite_update()` to avoid embedding bracket notation:
```python
# Before
newparams["{0}['{1}']".format(key, subkey)] = subval # → data_'x'_ after sanitization
# After
newparams["{0}__{1}_".format(key, subkey)] = subval # → data__x_ after sanitization
```
This would produce clean, quote-free bind parameter names that better reflect the column and sub-key relationship.
## Related Context
- The `visit_update` implementations in `compat/core10.py`, `compat/core14.py`, and `compat/core20.py` contain a deactivated SQLAlchemy sanity check ("Unconsumed column names") that was disabled precisely because of keys like `data['nested']` not being recognised. Fixing the key format here may also allow re-enabling or simplifying that workaround.
## References
- Discussed in PR #266: https://github.com/crate/sqlalchemy-cratedb/pull/266
- Specific comment: https://github.com/crate/sqlalchemy-cratedb/pull/266#discussion_r3380417798
- Requested by @bgunebakan
Contributor guide
Assessment
This issue has not been assessed yet.