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)
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()