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.")
def map_field_attr(table_name, field_identifier, column_type):
    assert(field_identifier != None)
    if not field_exists(table_name, field_identifier):
        # Create new Field
        new_column = Column(field_identifier, column_type)
        table = setobject_table_registry.lookup_by_table(table_name)
        new_column.create(table)

        # re-map setobject type
        map_tables(exclude_sys_tables=True)
 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)