[Checkins] SVN: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/m support for 'primary_key' parameter for mapper() method

Andreas Jung andreas at andreas-jung.com
Tue Apr 24 00:50:47 EDT 2007


Log message for revision 74691:
  support for 'primary_key' parameter for mapper() method
  

Changed:
  U   z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py
  U   z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py

-=-
Modified: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py
===================================================================
--- z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py	2007-04-23 23:49:48 UTC (rev 74690)
+++ z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/mapper.py	2007-04-24 04:50:46 UTC (rev 74691)
@@ -22,8 +22,6 @@
 class MappedClassBase(object):
     """ base class for all mapped classes """
 
-    __auto_mapped__ = False
-
     def __init__(self, **kw):
         """ accepts keywords arguments used for initialization of
             mapped attributes/columns.
@@ -39,7 +37,7 @@
     def __init__(self, metadata):
         self.metadata = metadata
 
-    def __call__(self, table, properties={}, cls=None):
+    def __call__(self, table, properties={}, cls=None, primary_key=None):
         """ Returns a tuple (mapped_class, table_class).
             'table' - sqlalchemy.Table to be mapped
 
@@ -54,8 +52,10 @@
         else:
             newCls = cls
 
-        mapper(newCls, table, properties=properties)
-        newCls.__auto_mapped__ = True
+        mapper(newCls, 
+               table, 
+               properties=properties, 
+               primary_key=primary_key)
         return newCls
 
 
@@ -104,10 +104,8 @@
 
             # check if the model contains an optional mapper class
             mapper_class = None
-            auto_mapped = False
             if self._model.has_key(name):           
                 mapper_class = self._model[name].get('mapper_class')
-                auto_mapped = getattr(mapper_class, '__auto_mapped__', False)
 
 
             # use auto-introspected table dependencies for creating
@@ -143,6 +141,10 @@
 
                 # add the mapper as relation to the properties dict
                 properties[table_refname] = relation(table_ref_mapper)
+
+
+            # pre-configured primary_key parameter?
+            primary_key = self._model.get(name, {}).get('primary_key')
        
             # create a mapper and cache it 
 
@@ -150,8 +152,10 @@
                 mapper = mapper_class
 
             else:
+
                 mapper =  self._mapper_factory(table, 
-                                               properties=properties, 
+                                               properties=properties,
+                                               primary_key=primary_key, 
                                                cls=mapper_class)
             self._registerMapper(mapper, name)
 

Modified: z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py
===================================================================
--- z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py	2007-04-23 23:49:48 UTC (rev 74690)
+++ z3c.sqlalchemy/trunk/src/z3c/sqlalchemy/model.py	2007-04-24 04:50:46 UTC (rev 74691)
@@ -35,7 +35,7 @@
             self.add(**d)
 
 
-    def add(self, name, table=None, mapper_class=None, relations=None, autodetect_relations=False, table_name=None):
+    def add(self, name, table=None, mapper_class=None, relations=None, autodetect_relations=False, table_name=None, primary_key=None):
         """ 'name'  -- name of table (no schema support so far!)
 
             'table' -- a sqlalchemy.Table instance (None, for autoloading)
@@ -51,6 +51,9 @@
 
             'table_name' -- optional full name of a table (e.g. 'someschema.sometable') if
             you want to use 'name' as alias for the table.
+
+            'primary_key' -- a sequence of strings used as primary keys for a mapper
+            (basically necessary when auto-loading a view as Table())
         """
 
         if table is not None and not isinstance(table, sqlalchemy.Table):
@@ -75,6 +78,7 @@
                       'mapper_class' : mapper_class,
                       'autodetect_relations' : autodetect_relations,
                       'table_name' : table_name,
+                      'primary_key' : primary_key,
                      }
 
 



More information about the Checkins mailing list