nodes: 0, module; 1, function_definition; 2, function_name:sort_models; 3, parameters; 4, block; 5, identifier:self; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, while_statement; 13, expression_statement; 14, return_statement; 15, comment:"""Sorts the database models appropriately based on their relationships so that we load our data
          in the appropriate order.
  
          Returns:
              A sorted list containing the names of the models.
          """; 16, assignment; 17, call; 18, assignment; 19, assignment; 20, assignment; 21, identifier:swapped; 22, block; 23, call; 24, identifier:model_names; 25, identifier:model_names; 26, list_comprehension; 27, attribute; 28, argument_list; 29, identifier:model_count; 30, call; 31, identifier:swapped; 32, True; 33, identifier:sort_round; 34, integer:0; 35, expression_statement; 36, expression_statement; 37, expression_statement; 38, for_statement; 39, comment:# we're done here (no changes after this sorting round); 40, if_statement; 41, expression_statement; 42, attribute; 43, argument_list; 44, attribute; 45, for_in_clause; 46, if_clause; 47, identifier:logger; 48, identifier:debug; 49, string:"Unsorted models: %s"; 50, identifier:model_names; 51, identifier:len; 52, argument_list; 53, augmented_assignment; 54, call; 55, assignment; 56, identifier:i; 57, call; 58, block; 59, comparison_operator:model_names == sorted_models; 60, block; 61, assignment; 62, identifier:logger; 63, identifier:debug; 64, string:"Sorted models: %s (%d rounds)"; 65, identifier:model_names; 66, identifier:sort_round; 67, identifier:table; 68, identifier:name; 69, identifier:table; 70, attribute; 71, comparison_operator:table.name in self.models; 72, identifier:model_names; 73, identifier:sort_round; 74, integer:1; 75, attribute; 76, argument_list; 77, identifier:sorted_models; 78, list; 79, identifier:range; 80, argument_list; 81, expression_statement; 82, comment:# check if this model has any dependencies which haven't been taken care of in this round; 83, for_statement; 84, if_statement; 85, identifier:model_names; 86, identifier:sorted_models; 87, expression_statement; 88, identifier:model_names; 89, identifier:sorted_models; 90, attribute; 91, identifier:sorted_tables; 92, attribute; 93, attribute; 94, identifier:logger; 95, identifier:debug; 96, string; 97, identifier:sort_round; 98, identifier:model_names; 99, identifier:model_count; 100, assignment; 101, identifier:foreign_model_name; 102, attribute; 103, block; 104, comparison_operator:model.name not in sorted_models; 105, block; 106, assignment; 107, attribute; 108, identifier:metadata; 109, identifier:table; 110, identifier:name; 111, identifier:self; 112, identifier:models; 113, string_content:Sorting round: %d (%s); 114, identifier:model; 115, subscript; 116, identifier:model; 117, identifier:foreign_models; 118, if_statement; 119, attribute; 120, identifier:sorted_models; 121, expression_statement; 122, identifier:swapped; 123, False; 124, identifier:self; 125, identifier:Base; 126, attribute; 127, subscript; 128, comparison_operator:foreign_model_name not in sorted_models; 129, block; 130, identifier:model; 131, identifier:name; 132, call; 133, identifier:self; 134, identifier:models; 135, identifier:model_names; 136, identifier:i; 137, identifier:foreign_model_name; 138, identifier:sorted_models; 139, expression_statement; 140, attribute; 141, argument_list; 142, call; 143, identifier:sorted_models; 144, identifier:append; 145, attribute; 146, attribute; 147, argument_list; 148, identifier:model; 149, identifier:name; 150, identifier:sorted_models; 151, identifier:append; 152, identifier:foreign_model_name
code: def sort_models(self):
          """Sorts the database models appropriately based on their relationships so that we load our data
          in the appropriate order.
  
          Returns:
              A sorted list containing the names of the models.
          """
          model_names = [
              table.name for table in self.Base.metadata.sorted_tables if table.name in self.models
          ]
          logger.debug("Unsorted models: %s", model_names)
          model_count = len(model_names)
  
          swapped = True
          sort_round = 0
          while swapped:
              sort_round += 1
              logger.debug('Sorting round: %d (%s)', sort_round, model_names)
  
              sorted_models = []
              for i in range(model_count):
                  model = self.models[model_names[i]]
                  # check if this model has any dependencies which haven't been taken care of in this round
                  for foreign_model_name in model.foreign_models:
                      if foreign_model_name not in sorted_models:
                          sorted_models.append(foreign_model_name)
  
                  if model.name not in sorted_models:
                      sorted_models.append(model.name)
  
              # we're done here (no changes after this sorting round)
              if model_names == sorted_models:
                  swapped = False
  
              model_names = sorted_models
  
          logger.debug("Sorted models: %s (%d rounds)", model_names, sort_round)
          return model_names