]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
synonym does not create the proxying behavior unless the flag 'proxy=True' is set up
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Oct 2006 18:11:21 +0000 (18:11 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Oct 2006 18:11:21 +0000 (18:11 +0000)
doc/build/content/adv_datamapping.txt
lib/sqlalchemy/orm/__init__.py
lib/sqlalchemy/orm/properties.py
test/orm/mapper.py

index 5b16c907252a8d813d19de741bc2ce55bc4ef07b..e46387c162ad7a397febc09a8c376700caf67f12 100644 (file)
@@ -84,7 +84,7 @@ A common request is the ability to create custom class properties that override
        '_email': mytable.c.email
     })
 
-It is also possible to use the `select_by` and `get_by` functions on `Query` using the original property name, by establishing a `synonym`:
+It is also possible to route the the `select_by` and `get_by` functions on `Query` using the new property name, by establishing a `synonym`:
 
     {python}
     mapper(MyClass, mytable, proeprties = {
@@ -99,6 +99,20 @@ It is also possible to use the `select_by` and `get_by` functions on `Query` usi
     # now you can select_by(email)
     result = session.query(MyClass).select_by(email='john@smith.com')
 
+Synonym can be established with the flag "proxy=True", to create a class-level proxy to the actual property:
+
+    {python}
+    mapper(MyClass, mytable, proeprties = {
+        '_email': mytable.c.email
+        'email' : synonym('_email', proxy=True)
+    })
+
+    x = MyClass()
+    x.email = 'john@doe.com'
+    
+    >>> x._email
+    'john@doe.com'
+    
 The `synonym` keyword is currently an [Alpha Feature][alpha_api].
 
 #### Custom List Classes {@name=customlist}
index d2d8bd53277e447092be4fe558cdbe5bf60c7d32..2d9a4e845118e4e233060f3badf54d756d6434c1 100644 (file)
@@ -45,9 +45,9 @@ def mapper(class_, table=None, *args, **params):
     """return a new Mapper object."""
     return Mapper(class_, table, *args, **params)
 
-def synonym(name):
+def synonym(name, proxy=False):
     """set up 'name' as a synonym to another MapperProperty."""
-    return properties.SynonymProperty(name)
+    return properties.SynonymProperty(name, proxy=proxy)
     
 def clear_mappers():
     """remove all mappers that have been created thus far.  when new mappers are 
index d409352e45c62c008ff90301688cfa1a3ae3c331..768b519593132ce249b79115b25ace46d350790f 100644 (file)
@@ -21,13 +21,16 @@ from interfaces import *
 
 
 class SynonymProperty(MapperProperty):
-    def __init__(self, name):
+    def __init__(self, name, proxy=False):
         self.name = name
+        self.proxy = proxy
     def setup(self, querycontext, **kwargs):
         pass
     def execute(self, selectcontext, instance, row, identitykey, isnew):
         pass
     def do_init(self):
+        if not self.proxy:
+            return
         class SynonymProp(object):
             def __set__(s, obj, value):
                 setattr(obj, self.name, value)
index 9b6717010cfc6f5655ec858151e89d4f50e06718..6a26c8b4be97d0dbb63018b14448b57dc391ef9a 100644 (file)
@@ -354,8 +354,8 @@ class MapperTest(MapperSuperTest):
         sess = create_session()
         mapper(User, users, properties = dict(
             addresses = relation(mapper(Address, addresses), lazy = True),
-            uname = synonym('user_name'),
-            adlist = synonym('addresses')
+            uname = synonym('user_name', proxy=True),
+            adlist = synonym('addresses', proxy=True)
         ))
         
         u = sess.query(User).get_by(uname='jack')