]> 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:44:16 +0000 (18:44 -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
(cherry picked from commit 7afcb39a962d383e5ba04179c1b13131fb08a787)

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 30bf72385c6065d272141d4fd04398a199833d0b..a0f24db282989c1dd7225491ffcece3f0c3f14fb 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 47eab7318dad9a97a8e0ce0e3ed988ecc7ec1499..16ad4a1c5ecfe8a7c166557d682f1d5aa0c9c2e1 100644 (file)
@@ -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()