Ejemplo n.º 1
0
 def check_create_fk(self, from_table_id, to_table_id, ignoreexisting=False):
     from_type = setobject_type_registry.lookup_by_table(from_table_id)        
     to_type = setobject_type_registry.lookup_by_table(to_table_id)        
     pk = to_type.get_primary_key_attr_name()
     # Now add foreign key if not existant yet
     if not field_exists(from_table_id, self.foreignkeycol):
         col = Column(
             self.foreignkeycol,
             getattr(to_type.get_table_class().c, pk).type,
             ForeignKey(to_table_id + "." + pk),
         )
         col.create(from_type.get_table_class())
        
         # The foreign key column has been newly created
         Session().flush()
         # deferred import
         from p2.datashackle.core.models.mapping import map_tables
         map_tables(exclude_sys_tables=True)
     else:
         # it exists, check whether it is what we want or something else
         fkset = getattr(from_type.get_table_class().c, self.foreignkeycol).foreign_keys
         if len(fkset) > 0:
             for fk in fkset:
                 if str(fk.column) == to_table_id + "." + pk \
                         and ignoreexisting == True:
                     return # this is what we want! fine.
             raise UserException("A relation with a similar Data Field Name but targetting the table '" + \
                 str(fk.column).split('.',1)[0] + "' already exists. Please use another Data Field Name.")
         raise UserException("The column '" + self.foreignkeycol + "' in the table '" + to_table_id + \
             "' does already exist. Please choose a unique Data Field Name that doesn't collide with existing data columns.")
Ejemplo n.º 2
0
 def _create_adjacency_list_relation(self):
     if not field_exists(self.target_table, self.foreignkeycol):
         target = setobject_type_registry.lookup_by_table(self.target_table)
         pk = target.get_primary_key_attr_name()
         col = Column(
             self.foreignkeycol,
             getattr(target.get_table_class().c, pk).type,
             ForeignKey(self.target_table + "." + pk),
         )
         col.create(target.get_table_class())
         
         # update session and remap table
         #Session().flush()
         
         # re-map user tables with newly created linkage 
         from p2.datashackle.core.models.mapping import map_tables
         map_tables(exclude_sys_tables=True)
Ejemplo n.º 3
0
def create_basemodel_type(class_name, table_name, do_mapping=True):
    """Create a setobject derived ORM type at runtime."""

    # The third parameter of builtin function 'type' is a dictionary which becomes the class' __dict__. We define the __init__
    # function that gets called whenever the setobject type is instantiated and its purpose is to call the
    # base class' (ModelBase) __init__ function.
    setobject_type = type(class_name, (ModelBase, ), {'__init__': lambda x: super(setobject_type, x).__init__()})

    setobject_type.table_name = table_name

    # Register setobject class type
    setobject_type_registry.register_type(setobject_type, 1)

    # map class to table
    if do_mapping == True:
        # deferred import
        from p2.datashackle.core.models.mapping import map_tables
        map_tables(exclude_sys_tables=True)

    return setobject_type