Exemple #1
0
 def get_tables_for_bind(self, bind=None):
     """Returns a list of all tables relevant for a bind."""
     result = []
     for table in itervalues(self.Model.metadata.tables):
         if table.info.get('bind_key') == bind:
             result.append(table)
     return result
Exemple #2
0
def _should_set_tablename(bases, d):
    """Check what values are set by a class and
    its bases to determine if a
    tablename should be automatically generated.

    The class and its bases are checked in order
    of precedence: the class itself then each base
    in the order they were given at class definition.

    Abstract classes do not generate a tablename,
    although they may have set
    or inherited a tablename elsewhere.

    If a class defines a tablename or table,
    a new one will not be generated.
    Otherwise, if the class defines a primary key,
    a new name will be generated.

    This supports:

    * Joined table inheritance without explicitly
      naming sub-models.
    * Single table inheritance.
    * Inheriting from mixins or abstract models.

    :param bases: base classes of new class
    :param d: new class dict
    :return: True if tablename should be set
    """

    if '__tablename__' in d or '__table__' in d or '__abstract__' in d:
        return False

    if any(v.primary_key for v in itervalues(d)
           if isinstance(v, sqlalchemy.Column)):
        return True

    for base in bases:
        if hasattr(base, '__tablename__') or hasattr(base, '__table__'):
            return False

        for name in dir(base):
            attr = getattr(base, name)

            if isinstance(attr, sqlalchemy.Column) and attr.primary_key:
                return True