""" This example creates sample crate and two modules. Selects them from DB and delete them after """ import sys from ttdb.model import obtain_session, Crate, Module if __name__ == "__main__": #get connection string if len(sys.argv) <= 1: exit("Provide connection string for database. For sqlite use 'sqlite:///' (Note 3 slashes)") session = obtain_session(sys.argv[1]) #Create crate and 3 modules crate = Crate() crate.name = "TEST-CRATE" module_1 = Module() module_1.slot = 1 module_1.type = "test-crate-1" module_2 = Module() module_2.slot = 2 module_2.type = "test-crate-2" module_3 = Module() module_3.slot = 3 module_3.type = "test-crate-3" #Add module to crate WAY 1 crate.modules.append(module_1) #Add module to crate WAY 2 module_2.crate = crate module_3.crate = crate #Now commit changes session.add(crate) session.add(module_1) session.add(module_2) session.add(module_3) session.commit() selected_crate = session.query(Crate).filter(Crate.name == crate.name).first() print("{}".format(crate.name)) for module in selected_crate.modules: print(" {} ==> {} (id={})".format(module.slot, module.type, module.id)) #delete everyting session.delete(crate) #modules deleted automaticly because of cascade delition set in Crate class session.commit()