Anonymous avatar Anonymous committed a6cf1e4

There were two significant changes in this commit:

* Added implicit primary keys to ActiveMapper. Now, if you do not speicfy a
primary key on your objects when declaring them, an Integer primary key
called `id` will automatically be added to your objects for you.

* Commented out a large chunk of the process_relationships function that
should no longer be necessary thanks to some of the deferred mapper
compilation that was added in SQLAlchemy 0.2.3. I left it in the code, but
commented it out just in case this change causes a problem in someone's
else's code and I can put it back in if needed.

Comments (0)

Files changed (2)

lib/sqlalchemy/ext/activemapper.py

 from sqlalchemy             import create_session, relation, mapper, \
-join, DynamicMetaData, class_mapper, util
+                                   join, DynamicMetaData, class_mapper, \
+                                   util, Integer
 from sqlalchemy             import and_, or_
 from sqlalchemy             import Table, Column, ForeignKey
 from sqlalchemy.ext.sessioncontext import SessionContext
     # and make sure that we can find the related tables (they do not 
     # have to be processed yet, just defined), and we defer if we are 
     # not able to find any of the related tables
+    
+    # thanks to deferred mapper compilation, this loop should no longer 
+    # be necessary -- however, I will leave it here commented out until
+    # I get the feeling that its not causing problems for people.
+    '''
     for col in klass.columns:
         if col.foreign_key is not None:
             found = False
                 if not was_deferred: __deferred_classes__.add(klass)
                 defer = True
                 break
+    '''
     
     # if we are able to find all related and referred to tables, then
     # we can go ahead and assign the relationships to the class
             if isinstance(reldesc.order_by, list):
                 for itemno in range(len(reldesc.order_by)):
                     if isinstance(reldesc.order_by[itemno], str):
-                        reldesc.order_by[itemno] = getattr(relclass.c, reldesc.order_by[itemno])
+                        reldesc.order_by[itemno] = \
+                            getattr(relclass.c, reldesc.order_by[itemno])
             relations[propname] = relation(relclass.mapper,
                                            secondary=reldesc.secondary,
                                            backref=reldesc.backref, 
                              "__metadata__", metadata)
         
         if 'mapping' in dict:
+            found_pk = False
+            
             members = inspect.getmembers(dict.get('mapping'))
             for name, value in members:
                 if name == '__table__':
                 if name.startswith('__'): continue
                 
                 if isinstance(value, column):
+                    if value.primary_key == True: found_pk = True
+                        
                     if value.foreign_key:
                         col = Column(value.colname or name, 
                                      value.coltype,
                 if isinstance(value, relationship):
                     relations[name] = value
             
+            if not found_pk:
+                col = Column('id', Integer, primary_key=True)
+                cls.mapping.id = col
+                columns.append(col)
+            
             assert _metadata is not None, "No MetaData specified"
             
             ActiveMapperMeta.metadatas.add(_metadata)

test/ext/activemapper.py

         
         class Person(ActiveMapper):
             class mapping:
-                id          = column(Integer, primary_key=True)
                 full_name   = column(String)
                 first_name  = column(String)
                 middle_name = column(String)
         class Preferences(ActiveMapper):
             class mapping:
                 __table__        = 'preferences'
-                id               = column(Integer, primary_key=True)
                 favorite_color   = column(String)
                 personality_type = column(String)
 
         class Address(ActiveMapper):
             class mapping:
+                # note that in other objects, the 'id' primary key is 
+                # automatically added -- if you specify a primary key,
+                # then ActiveMapper will not add an integer primary key
+                # for you.
                 id          = column(Integer, primary_key=True)
                 type        = column(String)
                 address_1   = column(String)
         )
         self.assertEquals(len(results), 1)
 
-
     
 if __name__ == '__main__':
-    # go ahead and setup the database connection, and create the tables
-    
-    # launch the unit tests
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.