示例#1
0
    def __init__(self,
                 table,
                 association_tables,
                 inflect_engine,
                 detect_joined,
                 back_populate=(),
                 backrefs=()):
        super(ModelClass, self).__init__(table)
        self.name = self._tablename_to_classname(table.name, inflect_engine)
        self.children = []
        self.attributes = OrderedDict()
        self.back_populate = back_populate

        # Assign attribute names for columns
        for column in table.columns:
            self._add_attribute(column.name, column)

        # Add many-to-one relationships
        pk_column_names = set(col.name for col in table.primary_key.columns)
        for constraint in sorted(table.constraints,
                                 key=_get_constraint_sort_key):
            if isinstance(constraint, ForeignKeyConstraint):
                target_cls = self._tablename_to_classname(
                    constraint.elements[0].column.table.name, inflect_engine)
                if (detect_joined and self.parent_name == 'Base' and set(
                        _get_column_names(constraint)) == pk_column_names):
                    self.parent_name = target_cls
                else:
                    backref = [(tgt_name, br_name)
                               for tgt_name, br_name in backrefs
                               if tgt_name == target_cls]
                    relationship_ = ManyToOneRelationship(
                        self.name,
                        target_cls,
                        constraint,
                        inflect_engine,
                        backref=backref[0] if len(backref) > 0 else ())
                    self._add_attribute(relationship_.preferred_name,
                                        relationship_)

        # Add many-to-many relationships
        for association_table in association_tables:
            fk_constraints = [
                c for c in association_table.constraints
                if isinstance(c, ForeignKeyConstraint)
            ]
            fk_table_names = [
                fk_constraint.elements[0].column.table.name
                for fk_constraint in fk_constraints if
                fk_constraint.elements[0].column.table.name != self.table.name
            ]
            if fk_table_names:
                target_cls = self._tablename_to_classname(
                    fk_table_names[0], inflect_engine)
                relationship_ = ManyToManyRelationship(
                    self.name,
                    target_cls,
                    association_table,
                    inflect_engine,
                    back_populate=association_table.name in self.back_populate)
                self._add_attribute(relationship_.preferred_name,
                                    relationship_)
示例#2
0
 def __init__(self, source_cls, target_cls):
     super(Relationship, self).__init__()
     self.source_cls = source_cls
     self.target_cls = target_cls
     self.kwargs = OrderedDict()
     self.backref_name = _underscore(self.source_cls)
示例#3
0
 def __init__(self, source_cls, target_cls):
     super(Relationship, self).__init__()
     self.source_cls = source_cls
     self.target_cls = target_cls
     self.kwargs = OrderedDict()
示例#4
0
from sqlalchemy.util import OrderedDict

#databases = ('sqlite','postgres','mysql','oracle','mssql','firebird')
databases = ('sqlite', 'postgres', 'mysql', 'oracle', 'mssql')

# Map operation names to function names
operations = OrderedDict()
operations['upgrade'] = 'upgrade'
operations['downgrade'] = 'downgrade'