From: Chris Withers Date: Wed, 6 Oct 2010 11:04:25 +0000 (+0100) Subject: warn when two classes with the same name end up in the declarative registry X-Git-Tag: rel_0_6_5~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a376f3e2daec1950b42822e94a8357848beaa18a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git warn when two classes with the same name end up in the declarative registry --- diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 5860675272..57dff857ca 100755 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -1046,6 +1046,13 @@ def _as_declarative(cls, classname, dict_): for k, v in mapper_args.iteritems(): mapper_args[k] = column_copies.get(v,v) + + if classname in cls._decl_class_registry: + util.warn("The classname %r is already in the registry of this" + " declarative base, mapped to %r" % ( + classname, + cls._decl_class_registry[classname] + )) cls._decl_class_registry[classname] = cls our_stuff = util.OrderedDict() diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index 0202aa69fc..0a6b17e27a 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -634,7 +634,7 @@ class DeclarativeTest(DeclarativeTestBase): def test_table_args(self): def err(): - class Foo(Base): + class Foo1(Base): __tablename__ = 'foo' __table_args__ = ForeignKeyConstraint(['id'], ['foo.id' @@ -644,13 +644,13 @@ class DeclarativeTest(DeclarativeTestBase): assert_raises_message(sa.exc.ArgumentError, 'Tuple form of __table_args__ is ', err) - class Foo(Base): + class Foo2(Base): __tablename__ = 'foo' __table_args__ = {'mysql_engine': 'InnoDB'} id = Column('id', Integer, primary_key=True) - assert Foo.__table__.kwargs['mysql_engine'] == 'InnoDB' + assert Foo2.__table__.kwargs['mysql_engine'] == 'InnoDB' class Bar(Base): @@ -659,7 +659,7 @@ class DeclarativeTest(DeclarativeTestBase): {'mysql_engine': 'InnoDB'} id = Column('id', Integer, primary_key=True) - assert Bar.__table__.c.id.references(Foo.__table__.c.id) + assert Bar.__table__.c.id.references(Foo2.__table__.c.id) assert Bar.__table__.kwargs['mysql_engine'] == 'InnoDB' def test_expression(self): @@ -1084,6 +1084,21 @@ class DeclarativeTest(DeclarativeTestBase): )).one() eq_(rt, u1) + @testing.emits_warning( + "The classname 'Test' is already in the registry " + "of this declarative base, mapped to " + "" + ) + def test_duplicate_classes_in_base(self): + + class Test(Base): + __tablename__ = 'a' + id = Column(Integer, primary_key=True) + + class Test(Base): + __tablename__ = 'b' + id = Column(Integer, primary_key=True) + class DeclarativeInheritanceTest(DeclarativeTestBase): def test_we_must_copy_mapper_args(self): @@ -3084,4 +3099,4 @@ class DeclarativeMixinPropertyTest(DeclarativeTestBase): def test_relationship_primryjoin(self): self._test_relationship(True) - \ No newline at end of file +