Example #1
0
 def belongs_to(self,rel_table):
     """ Description:
             Creates the foreign_key for the table relation. 
             Remark: The old table is dropped. 
         input parameters:    
             rel_table (type:string) Name of the table to be related.
     """
     #
     # Now creating the foreign_key
     #
     fkey = powlib.pluralize(rel_table) + ".id"
     if fkey in self.__table__.foreign_keys:
         err_msg = " already has a belongs_to relation to table "
         print "Table ", self.__table__.name, err_msg , rel_table
         raise StandardError( "Table " + self.__table__.name +  err_msg +  rel_table)
     else:
         fkey = ""
         #cons = ForeignKeyConstraint([table.c.fkey], [othertable.c.id])
         modelname = string.capitalize(rel_table)
         #print " -- loading model: ", modelname
         rel_model = powlib.load_class(modelname, modelname)
         #col = rel_model.getColumn(self.__table__.name + "_id")
         #print rel_model.getColumns()
         #print str(CreateTable(rel_model.__table__))
         self.__table__.append_column(Column(rel_model.__table__.name + "_id", Integer, ForeignKey(rel_model.__table__.name +".id")))
         cts = str(CreateTable(self.__table__))
         create_table_ddl = DDL(cts)    
         print cts
         self.__table__.drop()
         self.pbo.getConnection().execute(create_table_ddl)
     return
def render_relation_migration(name, PARTS_DIR = powlib.PARTS_DIR, prefix_dir = "./"):
    """
    renders a migration for a relational link between tables / models
    Typical examples are A.has_many(B) and B.belongs_to(A)
    these are then added to the newly genrated migration file.
    
    @params name    =>  name of the migration. Must be rel_modelA_modelB
    @param PARTS_DIR:   A relative path to the stubs/partials dir from the executing script.
    @param prefix_dir:  A prefix path to be added to migrations making prefix_dir/migrations the target dir
    """
    splittxt = string.split(name, "_")
    model1 = splittxt[1]
    model2 = splittxt[2]
    
    print " -- generate_migration: relation migration for models: " + model1 +  " & " + model2
    print " -- following the naming convention rel_model1_model2"
    print " -- you gave:", name
    
    # add the auto generated (but can be safely edited) warning to the outputfile
    infile = open (os.path.normpath(PARTS_DIR + "/db_relation_migration_stub.part"), "r")
    ostr = infile.read()
    infile.close()
    
    # add a creation date
    ostr = ostr.replace( "#DATE", str(datetime.date.today() ))
    # add model1 import
    ostr = ostr.replace( "#IMPORT_MODEL1", "import " + model1)
    # add model2 import
    ostr = ostr.replace( "#IMPORT_MODEL2", "import " + model2)
    
    # add the example migration for this models
    ostr = ostr.replace( "#MODEL1", model1)
    ostr = ostr.replace( "#MODEL2_has_many", powlib.pluralize(model2))
    ostr = ostr.replace( "#MODEL2", model2)
    
    filename = write_migration( name, 
                                "relation between %s and %s" % (model1, model2),
                                prefix_dir,
                                ostr
                                )
    print  " -- created file:" + str(os.path.normpath(os.path.join(prefix_dir,filename)))
    return