summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/persistence.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-03-28 18:00:35 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-03-28 18:00:35 -0400
commitd6618e41195587291c4a82af0db43fbd993b6c77 (patch)
treea93e608f2a145621ef19dfc869b78806e9b2f5bd /lib/sqlalchemy/orm/persistence.py
parenta0329f71ad48b16ad9dad44d5a5cb2c920e569f1 (diff)
downloadsqlalchemy-d6618e41195587291c4a82af0db43fbd993b6c77.tar.gz
- Added new parameter :paramref:`.mapper.confirm_deleted_rows`. Defaults
to True, indicates that a series of DELETE statements should confirm that the cursor rowcount matches the number of primary keys that should have matched; this behavior had been taken off in most cases (except when version_id is used) to support the unusual edge case of self-referential ON DELETE CASCADE; to accomodate this, the message is now just a warning, not an exception, and the flag can be used to indicate a mapping that expects self-refererntial cascaded deletes of this nature. See also :ticket:`2403` for background on the original change. re: #2403 fix #3007
Diffstat (limited to 'lib/sqlalchemy/orm/persistence.py')
-rw-r--r--lib/sqlalchemy/orm/persistence.py39
1 files changed, 23 insertions, 16 deletions
diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py
index d62e803ee..1bd432f15 100644
--- a/lib/sqlalchemy/orm/persistence.py
+++ b/lib/sqlalchemy/orm/persistence.py
@@ -681,19 +681,14 @@ def _emit_delete_statements(base_mapper, uowtransaction, cached_connections,
expected = len(del_objects)
rows_matched = -1
+ only_warn = False
if connection.dialect.supports_sane_multi_rowcount:
c = connection.execute(statement, del_objects)
- # only do a row check if we have versioning turned on.
- # unfortunately, we *cannot* do a check on the number of
- # rows matched here in general, as there is the edge case
- # of a table that has a self-referential foreign key with
- # ON DELETE CASCADE on it, see #2403. I'm not sure how we can
- # resolve this, unless we require special configuration
- # to enable "count rows" for certain mappings, or to disable
- # it, or to based on it relationship(), not sure.
- if need_version_id:
- rows_matched = c.rowcount
+ if not need_version_id:
+ only_warn = True
+
+ rows_matched = c.rowcount
elif need_version_id:
if connection.dialect.supports_sane_rowcount:
@@ -713,12 +708,24 @@ def _emit_delete_statements(base_mapper, uowtransaction, cached_connections,
else:
connection.execute(statement, del_objects)
- if rows_matched > -1 and expected != rows_matched:
- raise orm_exc.StaleDataError(
- "DELETE statement on table '%s' expected to "
- "delete %d row(s); %d were matched." %
- (table.description, expected, rows_matched)
- )
+ if base_mapper.confirm_deleted_rows and \
+ rows_matched > -1 and expected != rows_matched:
+ if only_warn:
+ util.warn(
+ "DELETE statement on table '%s' expected to "
+ "delete %d row(s); %d were matched. Please set "
+ "confirm_deleted_rows=False within the mapper "
+ "configuration to prevent this warning." %
+ (table.description, expected, rows_matched)
+ )
+ else:
+ raise orm_exc.StaleDataError(
+ "DELETE statement on table '%s' expected to "
+ "delete %d row(s); %d were matched. Please set "
+ "confirm_deleted_rows=False within the mapper "
+ "configuration to prevent this warning." %
+ (table.description, expected, rows_matched)
+ )
def _finalize_insert_update_commands(base_mapper, uowtransaction,
states_to_insert, states_to_update):