From: Mike Bayer Date: Fri, 6 Sep 2019 22:40:46 +0000 (-0400) Subject: Catch set_parent_w_dispatch missing X-Git-Tag: rel_1_3_9~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=da3c6211864cb2c49a8a702816b4f7c9e4900795;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Catch set_parent_w_dispatch missing Added an explicit error message for the case when objects passed to :class:`.Table` are not :class:`.SchemaItem` objects, rather than resolving to an attribute error. Fixes: #4847 Change-Id: I4dcdcee86b64c85ccf12e2ddc3d638563d307991 (cherry picked from commit 7afcb39a962d383e5ba04179c1b13131fb08a787) --- diff --git a/doc/build/changelog/unreleased_13/4847.rst b/doc/build/changelog/unreleased_13/4847.rst new file mode 100644 index 0000000000..2b0ef23797 --- /dev/null +++ b/doc/build/changelog/unreleased_13/4847.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: usecase, sql + :tickets: 4847 + + Added an explicit error message for the case when objects passed to + :class:`.Table` are not :class:`.SchemaItem` objects, rather than resolving + to an attribute error. + diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 30bf72385c..a0f24db282 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -104,7 +104,15 @@ class SchemaItem(SchemaEventTarget, visitors.Visitable): for item in args: if item is not None: - item._set_parent_with_dispatch(self) + try: + spwd = item._set_parent_with_dispatch + except AttributeError: + raise exc.ArgumentError( + "'SchemaItem' object, such as a 'Column' or a " + "'Constraint' expected, got %r" % item + ) + else: + spwd(self) def get_children(self, **kwargs): """used to allow SchemaVisitor access""" diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 47eab7318d..16ad4a1c5e 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -1466,6 +1466,37 @@ class TableTest(fixtures.TestBase, AssertsCompiledSQL): t.info["bar"] = "zip" assert t.info["bar"] == "zip" + def test_invalid_objects(self): + assert_raises_message( + tsa.exc.ArgumentError, + "'SchemaItem' object, such as a 'Column' or a " + "'Constraint' expected, got <.*ColumnClause at .*; q>", + Table, + "asdf", + MetaData(), + tsa.column("q", Integer), + ) + + assert_raises_message( + tsa.exc.ArgumentError, + r"'SchemaItem' object, such as a 'Column' or a " + r"'Constraint' expected, got String\(\)", + Table, + "asdf", + MetaData(), + String(), + ) + + assert_raises_message( + tsa.exc.ArgumentError, + "'SchemaItem' object, such as a 'Column' or a " + "'Constraint' expected, got 12", + Table, + "asdf", + MetaData(), + 12, + ) + def test_reset_exported_passes(self): m = MetaData()