def build_parent_relationship(self):
        """
        Builds a relationship between currently built history class and
        parent class (the model whose history the currently build history
        class represents).
        """
        conditions = []
        foreign_keys = []
        for primary_key in primary_keys(self.model):
            conditions.append(
                getattr(self.model, primary_key.name)
                ==
                getattr(self.extension_class, primary_key.name)
            )
            foreign_keys.append(
                getattr(self.extension_class, primary_key.name)
            )

        # We need to check if versions relation was already set for parent
        # class.
        if not hasattr(self.model, 'versions'):
            self.model.versions = sa.orm.relationship(
                self.extension_class,
                primaryjoin=sa.and_(*conditions),
                foreign_keys=foreign_keys,
                lazy='dynamic',
                backref=sa.orm.backref(
                    'version_parent'
                ),
                viewonly=True
            )
示例#2
0
 def build_foreign_key(self):
     names = [column.name for column in primary_keys(self.model)]
     return sa.schema.ForeignKeyConstraint(
         names,
         [
             '%s.%s' % (self.model.__tablename__, name)
             for name in names
         ],
         ondelete='CASCADE'
     )
示例#3
0
 def build_reflected_primary_keys(self):
     columns = []
     for column in primary_keys(self.model):
         # Make a copy of the column so that it does not point to wrong
         # table.
         column_copy = column.copy()
         # Remove unique constraints
         column_copy.unique = False
         columns.append(column_copy)
     return columns