Repository navigation
Open
Labels
Milestone
Description
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()anddelete(), keyword arguments are column values that are merged into the row dict. Inupsert(), they are rules for updating columns on conflict (True,False/Noneor an SQL expression). - Parameter names collide with column names. Since columns are passed as keyword arguments, columns named
table,roworkeynamecannot be passed that way. Forupsert(), 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 NOTHINGwithout a conflict target, conflicts on a constraint name (needed for exclusion constraints), andWHEREclauses 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 aDatabaseErrorin this case, anddelete()returns the number of deleted rows. - Leftovers of the OID design. The
oid(table)keys, theoidkeyword for identifying rows, and thewith_oidparameter ofget_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
**kwfor column values only, and move the update rules ofupsert()to a parameter, e.g.upsert(table, row, keyname=None, update=None)whereupdateis a list of columns or a dict of columns toTrue/False/SQL expressions, and an empty list meansDO NOTHING. - Return a new dict instead of modifying the passed one, or make this an option.
- Let
update()signal a missing row likeget()(raise an exception), or returnNone. - Support conflict targets given as columns or as a constraint name, and optionally
DO NOTHINGwithout 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=...)andon_conflict_do_nothing(). Django hasbulk_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
Cito commentedon Nov 23, 2026
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
DeprecationWarningwhere 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.