From: Jonathan LaCour Date: Thu, 29 Jun 2006 23:29:37 +0000 (+0000) Subject: There were two significant changes in this commit: X-Git-Tag: rel_0_2_5~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4e3aa8829acfe824ccb6628be4a1f6af8e220349;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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. --- diff --git a/lib/sqlalchemy/ext/activemapper.py b/lib/sqlalchemy/ext/activemapper.py index 22087e5ead..132c93e75f 100644 --- a/lib/sqlalchemy/ext/activemapper.py +++ b/lib/sqlalchemy/ext/activemapper.py @@ -1,5 +1,6 @@ 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 @@ -109,6 +110,11 @@ def process_relationships(klass, was_deferred=False): # 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 @@ -121,6 +127,7 @@ def process_relationships(klass, was_deferred=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 @@ -133,7 +140,8 @@ def process_relationships(klass, was_deferred=False): 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, @@ -172,6 +180,8 @@ class ActiveMapperMeta(type): "__metadata__", metadata) if 'mapping' in dict: + found_pk = False + members = inspect.getmembers(dict.get('mapping')) for name, value in members: if name == '__table__': @@ -185,6 +195,8 @@ class ActiveMapperMeta(type): 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, @@ -202,6 +214,11 @@ class ActiveMapperMeta(type): 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) diff --git a/test/ext/activemapper.py b/test/ext/activemapper.py index ef6886a785..1bb93dd634 100644 --- a/test/ext/activemapper.py +++ b/test/ext/activemapper.py @@ -13,7 +13,6 @@ class testcase(testbase.PersistTest): class Person(ActiveMapper): class mapping: - id = column(Integer, primary_key=True) full_name = column(String) first_name = column(String) middle_name = column(String) @@ -43,12 +42,15 @@ class testcase(testbase.PersistTest): 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) @@ -216,10 +218,6 @@ class testcase(testbase.PersistTest): ) 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()