From: Mike Bayer Date: Tue, 13 Jan 2009 15:45:59 +0000 (+0000) Subject: - It's an error to add new Column objects to a declarative class X-Git-Tag: rel_0_5_1~16 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3e3f309cf99b0123be4e7295891e5531b137e1fb;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - It's an error to add new Column objects to a declarative class that specified an existing table using __table__. --- diff --git a/CHANGES b/CHANGES index 7904085650..c7846b9c0d 100644 --- a/CHANGES +++ b/CHANGES @@ -73,6 +73,9 @@ CHANGES mapping than you'd normally get with explicit `mapper()` calls unless you set up the `exclude_properties` arguments explicitly. + + - It's an error to add new Column objects to a declarative class + that specified an existing table using __table__. - mysql - Added the missing keywords from MySQL 4.1 so they get escaped diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 70f50622d7..cf47279e8a 100644 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -478,7 +478,9 @@ def _as_declarative(cls, classname, dict_): *(tuple(cols) + tuple(args)), **table_kw) else: table = cls.__table__ - + if cols: + raise exceptions.ArgumentError("Can't add additional columns when specifying __table__") + mapper_args = getattr(cls, '__mapper_args__', {}) if 'inherits' not in mapper_args: inherits = cls.__mro__[1] @@ -530,7 +532,7 @@ def _as_declarative(cls, classname, dict_): mapper_args['exclude_properties'] = exclude_properties = \ set([c.key for c in inherited_table.c if c not in inherited_mapper._columntoproperty]) exclude_properties.difference_update([c.key for c in cols]) - + cls.__mapper__ = mapper_cls(cls, table, properties=our_stuff, **mapper_args) class DeclarativeMeta(type): diff --git a/test/ext/declarative.py b/test/ext/declarative.py index 71505e48f5..c9477b5d85 100644 --- a/test/ext/declarative.py +++ b/test/ext/declarative.py @@ -63,6 +63,14 @@ class DeclarativeTest(DeclarativeTestBase): class User(Base): id = Column('id', Integer, primary_key=True) self.assertRaisesMessage(sa.exc.InvalidRequestError, "does not have a __table__", go) + + def test_cant_add_columns(self): + t = Table('t', Base.metadata, Column('id', Integer, primary_key=True)) + def go(): + class User(Base): + __table__ = t + foo = Column(Integer, primary_key=True) + self.assertRaisesMessage(sa.exc.ArgumentError, "add additional columns", go) def test_undefer_column_name(self): # TODO: not sure if there was an explicit