__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__
=======================================
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 = (), {}
'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():