cleverdevil  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.

  • Participants
  • Parent commits 30584bc
  • Branches default

Comments (0)

Files changed (2)

File lib/sqlalchemy/ext/

 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
+    '''
     # 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,
                              "__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, 
                 if isinstance(value, relationship):
                     relations[name] = value
+            if not found_pk:
+                col = Column('id', Integer, primary_key=True)
+       = col
+                columns.append(col)
             assert _metadata is not None, "No MetaData specified"

File test/ext/

         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