]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Catch set_parent_w_dispatch missing
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 6 Sep 2019 22:40:46 +0000 (18:40 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 6 Sep 2019 22:43:58 +0000 (18:43 -0400)
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

doc/build/changelog/unreleased_13/4847.rst [new file with mode: 0644]
lib/sqlalchemy/sql/schema.py
test/sql/test_metadata.py

diff --git a/doc/build/changelog/unreleased_13/4847.rst b/doc/build/changelog/unreleased_13/4847.rst
new file mode 100644 (file)
index 0000000..2b0ef23
--- /dev/null
@@ -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.
+
index d9667e4455e63e28de03d68a4299c5955f1c5182..69fc9408f72e8ad0dc4656d9473d07bfd8cb7a14 100644 (file)
@@ -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"""
index f3fa008c596c66f19d33cf71db14668063afb670..ad251d1b8142396714a9e91ec5071df221776d24 100644 (file)
@@ -1465,6 +1465,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()