From 87929a9c55163ebcd7edd031d222c7f115922842 Mon Sep 17 00:00:00 2001 From: Jan Vollmer Date: Wed, 22 Nov 2023 13:16:27 -0500 Subject: [PATCH] add local column to dependency rule error message Improved the error message produced when the unit of work process sets the value of a primary key column to NULL due to a related object with a dependency rule on that column being deleted, to include not just the destination object and column name but also the source column from which the NULL value is originating. Pull request courtesy Jan Vollmer. Fixes: #10668 Closes: #10669 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10669 Pull-request-sha: d7f9952b81394d585e55dad6d6f355aaa54f599e Change-Id: I210317f8777ff07d9eec674b79f2140523af38d0 --- doc/build/changelog/unreleased_20/10668.rst | 9 +++++++++ lib/sqlalchemy/orm/sync.py | 5 +++-- test/orm/test_relationships.py | 7 ++++--- test/orm/test_sync.py | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 doc/build/changelog/unreleased_20/10668.rst diff --git a/doc/build/changelog/unreleased_20/10668.rst b/doc/build/changelog/unreleased_20/10668.rst new file mode 100644 index 0000000000..560aac85e9 --- /dev/null +++ b/doc/build/changelog/unreleased_20/10668.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, orm + :tickets: 10668 + + Improved the error message produced when the unit of work process sets the + value of a primary key column to NULL due to a related object with a + dependency rule on that column being deleted, to include not just the + destination object and column name but also the source column from which + the NULL value is originating. Pull request courtesy Jan Vollmer. diff --git a/lib/sqlalchemy/orm/sync.py b/lib/sqlalchemy/orm/sync.py index 036c26dd6b..dbe8fb7a25 100644 --- a/lib/sqlalchemy/orm/sync.py +++ b/lib/sqlalchemy/orm/sync.py @@ -86,8 +86,9 @@ def clear(dest, dest_mapper, synchronize_pairs): not in orm_util._none_set ): raise AssertionError( - "Dependency rule tried to blank-out primary key " - "column '%s' on instance '%s'" % (r, orm_util.state_str(dest)) + f"Dependency rule on column '{l}' " + "tried to blank-out primary key " + f"column '{r}' on instance '{orm_util.state_str(dest)}'" ) try: dest_mapper._set_state_attr_by_column(dest, dest.dict, r, None) diff --git a/test/orm/test_relationships.py b/test/orm/test_relationships.py index d6b886be15..969196ad8c 100644 --- a/test/orm/test_relationships.py +++ b/test/orm/test_relationships.py @@ -1334,7 +1334,8 @@ class CompositeSelfRefFKTest(fixtures.MappedTest, AssertsCompiledSQL): # this happens assert_raises_message( AssertionError, - "Dependency rule tried to blank-out primary key column " + "Dependency rule on column 'employee_t.company_id' " + "tried to blank-out primary key column " "'employee_t.company_id'", sess.flush, ) @@ -1669,7 +1670,7 @@ class FKsAsPksTest(fixtures.MappedTest): assert_raises_message( AssertionError, - "Dependency rule tried to blank-out " + "Dependency rule on column 'tableA.id' tried to blank-out " "primary key column 'tableB.id' on instance ", sess.flush, ) @@ -1696,7 +1697,7 @@ class FKsAsPksTest(fixtures.MappedTest): b1.a = None assert_raises_message( AssertionError, - "Dependency rule tried to blank-out " + "Dependency rule on column 'tableA.id' tried to blank-out " "primary key column 'tableB.id' on instance ", sess.flush, ) diff --git a/test/orm/test_sync.py b/test/orm/test_sync.py index c8f511f447..10d73cb8d6 100644 --- a/test/orm/test_sync.py +++ b/test/orm/test_sync.py @@ -145,7 +145,7 @@ class SyncTest( eq_(b1.obj().__dict__["id"], 8) assert_raises_message( AssertionError, - "Dependency rule tried to blank-out primary key " + "Dependency rule on column 't1.id' tried to blank-out primary key " "column 't2.id' on instance '