]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- the dictionary at the end of the __table_args__
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 Feb 2011 17:06:56 +0000 (12:06 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 Feb 2011 17:06:56 +0000 (12:06 -0500)
tuple is now optional.  [ticket:1468]

CHANGES
lib/sqlalchemy/ext/declarative.py
test/ext/test_declarative.py

diff --git a/CHANGES b/CHANGES
index 4d4ac726d1c5f8f878fa9bc26989b50d1282f999..9e026e0e99d4d9845480a3768391a19ce874b274 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -43,6 +43,9 @@ CHANGES
     and pulled in via name or object ref. 
     [ticket:2058]
 
+  - the dictionary at the end of the __table_args__
+    tuple is now optional.  [ticket:1468]
+
 0.7.0b1
 =======
 - Detailed descriptions of each change below are 
index 00c4aec3f62ae5cf81b9cd85d66beeb3b142e976..c8c464d0db9f93d858cd92b127d6edf5ffd44c8a 100755 (executable)
@@ -262,20 +262,26 @@ dictionary::
         __tablename__ = 'sometable'
         __table_args__ = {'mysql_engine':'InnoDB'}
 
-The other, a tuple of the form
-``(arg1, arg2, ..., {kwarg1:value, ...})``, which allows positional
-arguments to be specified as well (usually constraints):: 
+The other, a tuple, where each argument is positional
+(usually constraints)::
 
     class MyClass(Base):
         __tablename__ = 'sometable'
         __table_args__ = (
                 ForeignKeyConstraint(['id'], ['remote_table.id']),
                 UniqueConstraint('foo'),
-                {'autoload':True}
                 )
 
-Note that the keyword parameters dictionary is required in the tuple
-form even if empty.
+Keyword arguments can be specified with the above form by 
+specifying the last argument as a dictionary::
+
+    class MyClass(Base):
+        __tablename__ = 'sometable'
+        __table_args__ = (
+                ForeignKeyConstraint(['id'], ['remote_table.id']),
+                UniqueConstraint('foo'),
+                {'autoload':True}
+                )
 
 Using a Hybrid Approach with __table__
 =======================================
@@ -1004,14 +1010,10 @@ def _as_declarative(cls, classname, dict_):
             if isinstance(table_args, dict):
                 args, table_kw = (), table_args
             elif isinstance(table_args, tuple):
-                args = table_args[0:-1]
-                table_kw = table_args[-1]
-                if len(table_args) < 2 or not isinstance(table_kw, dict):
-                    raise exc.ArgumentError(
-                        "Tuple form of __table_args__ is "
-                        "(arg1, arg2, arg3, ..., {'kw1':val1, "
-                        "'kw2':val2, ...})"
-                    )
+                if isinstance(table_args[-1], dict):
+                    args, table_kw = table_args[0:-1], table_args[-1]
+                else:
+                    args, table_kw = table_args, {}
             else:
                 args, table_kw = (), {}
 
index 8c97fbf27b56cc73c830475e5a3074a9b3f57729..12d347f28a6827f7d396da65154568b86e2b9c46 100644 (file)
@@ -672,18 +672,16 @@ class DeclarativeTest(DeclarativeTestBase):
                               'Mapper Mapper|User|users could not '
                               'assemble any primary key', define)
 
-    def test_table_args_bad_format(self):
+    def test_table_args_no_dict(self):
 
-        def err():
-            class Foo1(Base):
+        class Foo1(Base):
 
-                __tablename__ = 'foo'
-                __table_args__ = ForeignKeyConstraint(['id'], ['foo.id'
-                        ]),
-                id = Column('id', Integer, primary_key=True)
+            __tablename__ = 'foo'
+            __table_args__ = ForeignKeyConstraint(['id'], ['foo.bar']),
+            id = Column('id', Integer, primary_key=True)
+            bar = Column('bar', Integer)
 
-        assert_raises_message(sa.exc.ArgumentError,
-                              'Tuple form of __table_args__ is ', err)
+        assert Foo1.__table__.c.id.references(Foo1.__table__.c.bar)
 
     def test_table_args_type(self):
         def err():