          if key[0] in self.migrated_apps:
              try:
                  if key[1] == "__first__":
                      return list(self.graph.root_nodes(key[0]))[0]
                  else:
                      return list(self.graph.leaf_nodes(key[0]))[0]
              except IndexError:
                  if self.ignore_no_migrations:
                      return None
                  else:
 
</s> remove                 self.graph.add_dependency(migration, key, parent)
 </s> add                 try:
                    self.graph.add_dependency(migration, key, parent)
                except NodeNotFoundError as e:
                    # Since we added "key" to the nodes before this implies
                    # "parent" is not in there. To make the raised exception
                    # more understandable we check if parent could have been
                    # replaced but hasn't (eg partially applied squashed
                    # migration)
                    _reraise_missing_dependency(migration, parent, e) </s> remove             raise ValueError("Node %r not a valid node" % (node, ))
 </s> add             raise NodeNotFoundError("Node %r not a valid node" % (node, ), node) </s> remove             raise ValueError("Node %r not a valid node" % (node, ))
 </s> add             raise NodeNotFoundError("Node %r not a valid node" % (node, ), node) </s> remove         "Gets the migration exactly named, or raises KeyError"
 </s> add         "Gets the migration exactly named, or raises `graph.NodeNotFoundError`" </s> remove                     self.graph.add_dependency(migration, key, parent)
 </s> add                     try:
                        self.graph.add_dependency(migration, key, parent)
                    except NodeNotFoundError as e:
                        # Since we added "key" to the nodes before this implies
                        # "parent" is not in there.
                        _reraise_missing_dependency(migration, parent, e)
                else:  # "__latest__"
          if key[0] in self.migrated_apps:
              try:
                  if key[1] == "__first__":
                      return list(self.graph.root_nodes(key[0]))[0]
                else:  # "__latest__"
                      return list(self.graph.leaf_nodes(key[0]))[0]
              except IndexError:
                  if self.ignore_no_migrations:
                      return None
                  else:
keep keep keep keep replace keep keep keep keep keep
 Fixed #23556 -- Raised a more meaningful error message when migrations refer to an unavailable node 
django/db/migrations/loader.py