Skip to content

Redesign the row methods of the DB wrapper #126

Open

Description

@Cito

The row methods of the DB wrapper (get(), insert(), update(), upsert(), delete()) have grown over time. Their API was designed around tables with OIDs, which no longer exist since PostgreSQL 12, and it has several inconsistencies that cannot be fixed without breaking changes. I'd like to redesign them in 7.0. This issue describes the problems and collects suggestions. It does not decide on the new API yet.

For 6.3, I'm adding a keyname parameter to update(), delete() and upsert() like the one in get(), so tables without a primary key can be used (#56), and removing the remaining OID support. These changes are backward compatible, but the problems below remain.

Problems

  • Keyword arguments mean different things. In insert(), update() and delete(), keyword arguments are column values that are merged into the row dict. In upsert(), they are rules for updating columns on conflict (True, False/None or an SQL expression).
  • Parameter names collide with column names. Since columns are passed as keyword arguments, columns named table, row or keyname cannot be passed that way. For upsert(), no update rule can be given for such a column at all.
  • Awkward "do nothing". To leave an existing row unchanged, you have to call upsert(table, row, **dict.fromkeys(row)). ON CONFLICT DO NOTHING without a conflict target, conflicts on a constraint name (needed for exclusion constraints), and WHERE clauses cannot be expressed.
  • The passed dict is modified in place and also returned. This side effect is surprising, and in upsert() it is the only way to get the result.
  • Errors pass silently. update() returns the row unchanged if no row matches the key, so the caller cannot tell whether anything was updated. get() raises a DatabaseError in this case, and delete() returns the number of deleted rows.
  • Leftovers of the OID design. The oid(table) keys, the oid keyword for identifying rows, and the with_oid parameter of get_attnames().
  • Table names are handled inconsistently, see Proper way of specifying the schema along with the table #67 (schema-qualified names, the * suffix for descendant tables).

Suggestions (to be discussed)

  • Pass column values only in the row (a dict or keyword arguments, but not both), and make all options keyword-only with names that are clearly not column names. Or the opposite: keep **kw for column values only, and move the update rules of upsert() to a parameter, e.g. upsert(table, row, keyname=None, update=None) where update is a list of columns or a dict of columns to True/False/SQL expressions, and an empty list means DO NOTHING.
  • Return a new dict instead of modifying the passed one, or make this an option.
  • Let update() signal a missing row like get() (raise an exception), or return None.
  • Support conflict targets given as columns or as a constraint name, and optionally DO NOTHING without a target.
  • Look at how others do it: psycopg 3 has no such helpers. SQLAlchemy has insert().on_conflict_do_update(index_elements=..., constraint=..., set_=..., where=...) and on_conflict_do_nothing(). Django has bulk_create(update_conflicts=True, unique_fields=..., update_fields=...).
  • Transition: new method names, or deprecation warnings in a 6.x release before changing the existing methods.

Activity

  1. added this to the 7.0 milestone on Nov 22, 2026
  2. modified the milestones: 7.0, 6.4 on Nov 23, 2026
  3. Cito commented on Nov 23, 2026

    @Cito
    MemberAuthor

    I moved this to 6.4, so that the redesign can happen in two steps: 6.4 adds the new API next to the existing methods and emits a DeprecationWarning where the old usage will change, and 7.0 removes the old behavior. The new API still has to be decided (see the suggestions above). I will open a separate 7.0 issue for the removal once it is decided. If a part cannot be added without breaking existing code, that part goes back to 7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions