From: Mike Bayer Date: Mon, 6 Feb 2006 01:00:02 +0000 (+0000) Subject: added cascade_mappers function. somewhat experimental ! X-Git-Tag: rel_0_1_0~47 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7052521d8315ec7ad67d151c2601d649998af3cb;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added cascade_mappers function. somewhat experimental ! --- diff --git a/lib/sqlalchemy/mapping/__init__.py b/lib/sqlalchemy/mapping/__init__.py index b9b9ee5a10..ce693b2fb8 100644 --- a/lib/sqlalchemy/mapping/__init__.py +++ b/lib/sqlalchemy/mapping/__init__.py @@ -136,4 +136,53 @@ def assign_mapper(class_, *args, **params): def delete(self): objectstore.delete(self) class_.commit = commit - class_.delete = delete \ No newline at end of file + class_.delete = delete + +def cascade_mappers(*classes_or_mappers): + """given a list of classes and/or mappers, identifies the foreign key relationships + between the given mappers or corresponding class mappers, and creates relation() + objects representing those relationships, including a backreference. Attempts to find + the "secondary" table in a many-to-many relationship as well. The names of the relations + will be a lowercase version of the related class. In the case of one-to-many or many-to-many, + the name will be "pluralized", which currently is based on the English language (i.e. an 's' or + 'es' added to it).""" + table_to_mapper = {} + for item in classes_or_mappers: + if isinstance(item, Mapper): + m = item + else: + klass = item + m = class_mapper(klass) + table_to_mapper[m.table] = m + def pluralize(name): + # oh crap, do we need locale stuff now + if name[-1] == 's': + return name + "es" + else: + return name + "s" + for table,mapper in table_to_mapper.iteritems(): + for fk in table.foreign_keys: + if fk.column.table is table: + continue + secondary = None + try: + m2 = table_to_mapper[fk.column.table] + except KeyError: + if len(fk.column.table.primary_key): + continue + for sfk in fk.column.table.foreign_keys: + if sfk.column.table is table: + continue + m2 = table_to_mapper.get(sfk.column.table) + secondary = fk.column.table + if m2 is None: + continue + if secondary: + propname = pluralize(m2.class_.__name__.lower()) + propname2 = pluralize(mapper.class_.__name__.lower()) + else: + propname = m2.class_.__name__.lower() + propname2 = pluralize(mapper.class_.__name__.lower()) + mapper.add_property(propname, relation(m2, secondary=secondary, backref=propname2)) + + \ No newline at end of file