]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Table objects declared in the MetaData can now be used
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 12 Sep 2009 20:28:10 +0000 (20:28 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 12 Sep 2009 20:28:10 +0000 (20:28 +0000)
in string expressions sent to primaryjoin/secondaryjoin/
secondary - the name is pulled from the MetaData of the
declarative base.  [ticket:1527]

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

diff --git a/CHANGES b/CHANGES
index d3a5fb2e17a73787b8ec20a9837eb258e4093f15..efa34454c944b40e2fd5683f8cbd51836f407c0c 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -504,6 +504,11 @@ CHANGES
      __table_args__ is passed as a tuple with no dict argument.
      Improved documentation.  [ticket:1468]
 
+   - Table objects declared in the MetaData can now be used
+     in string expressions sent to primaryjoin/secondaryjoin/
+     secondary - the name is pulled from the MetaData of the
+     declarative base.  [ticket:1527]
+     
    - A column can be added to a joined-table subclass after
      the class has been constructed (i.e. via class-level
      attribute assignment).  The column is added to the underlying
index c37211ac3dbb6f430d9f8f4523516dff130e4bb2..80faea36b2a9ca4394425d69ce2a049db068b38d 100644 (file)
@@ -600,9 +600,11 @@ def _deferred_relation(cls, prop):
         import sqlalchemy
         
         def access_cls(key):
-            try:
+            if key in cls._decl_class_registry:
                 return _GetColumns(cls._decl_class_registry[key])
-            except KeyError:
+            elif key in cls.metadata.tables:
+                return cls.metadata.tables[key]
+            else:
                 return sqlalchemy.__dict__[key]
 
         d = util.PopulateDict(access_cls)
index b64ad23e1c6f4a14703d9fb40237ed0c8055ddd4..16a0d697f45c3511a08e0ce9663690fce29c2db8 100644 (file)
@@ -175,7 +175,31 @@ class DeclarativeTest(DeclarativeTestBase):
         compile_mappers()
         eq_(str(User.addresses.property.primaryjoin), str(Address.user.property.primaryjoin))
         
+    def test_string_dependency_resolution_tables(self):
+        class User(Base, ComparableEntity):
+            __tablename__ = 'users'
+            id = Column(Integer, primary_key=True)
+            name = Column(String(50))
+            
+            props = relation("Prop", 
+                        secondary="user_to_prop", 
+                        primaryjoin="User.id==user_to_prop.c.user_id", 
+                        secondaryjoin="user_to_prop.c.prop_id==Prop.id", 
+                    backref="users")
+
+        class Prop(Base, ComparableEntity):
+            __tablename__ = 'props'
+            id = Column(Integer, primary_key=True)
+            name = Column(String(50))
         
+        user_to_prop = Table('user_to_prop', Base.metadata, 
+            Column('user_id', Integer, ForeignKey('users.id')),
+            Column('prop_id', Integer, ForeignKey('props.id')),
+        )
+
+        compile_mappers()
+        assert class_mapper(User).get_property("props").secondary is user_to_prop
+
     def test_uncompiled_attributes_in_relation(self):
         class Address(Base, ComparableEntity):
             __tablename__ = 'addresses'