summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/activemapper.py
diff options
context:
space:
mode:
authorJonathan LaCour <jonathan@cleverdevil.org>2006-06-29 23:29:37 +0000
committerJonathan LaCour <jonathan@cleverdevil.org>2006-06-29 23:29:37 +0000
commit4e3aa8829acfe824ccb6628be4a1f6af8e220349 (patch)
tree79d29782a2f1c861f51436dd0162aec17dc3a156 /lib/sqlalchemy/ext/activemapper.py
parent2c28effb75c4578d34c85ed79aaec079e0734ca2 (diff)
downloadsqlalchemy-4e3aa8829acfe824ccb6628be4a1f6af8e220349.tar.gz
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.
Diffstat (limited to 'lib/sqlalchemy/ext/activemapper.py')
-rw-r--r--lib/sqlalchemy/ext/activemapper.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/sqlalchemy/ext/activemapper.py b/lib/sqlalchemy/ext/activemapper.py
index 22087e5ea..132c93e75 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)