Skip to content

Refactor rewrite_update() for cleaner bind parameter key representation #270

@coderabbitai

Description

@coderabbitai

Summary

The rewrite_update() function in src/sqlalchemy_cratedb/compiler.py constructs bind parameter keys for partial ObjectType updates using the following format:

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:

# 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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions