From: Mike Bayer Date: Thu, 17 Feb 2011 17:06:56 +0000 (-0500) Subject: - the dictionary at the end of the __table_args__ X-Git-Tag: rel_0_7b2~1^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7c5fd7b22dd21ec1c1cac177b9ee611779903e3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - the dictionary at the end of the __table_args__ tuple is now optional. [ticket:1468] --- diff --git a/CHANGES b/CHANGES index 4d4ac726d1..9e026e0e99 100644 --- 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 diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 00c4aec3f6..c8c464d0db 100755 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -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 = (), {} diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index 8c97fbf27b..12d347f28a 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -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():