Results 1 to 3 of 3

Thread: Issue counting nodes or relationships when count is 0

  1. #1
    Join Date
    Sep 2006
    Location
    Hartford, CT
    Posts
    145

    Default Issue counting nodes or relationships when count is 0

    Hi all,

    Having a bit of an issue with Spring Datagraph.

    Given the following:

    Code:
    @NodeEntity
    User {
      
      // ...
    
      @Query("start user=(%start) match (user)-[:MADE_DONATION]->() return count(*)")
      private int totalDonations;
    
      public int getTotalDonations() {
        return totalDonations;
      }
    
      // ...
    
    }
    I get the following when invoking getTotalDonations() IF the user doesn't HAVE any such relationships.

    java.lang.IllegalStateException: Expected at least one result, got none.
    This seems wrong. Shouldn't I get back 0? Is there a better way to do what I'm trying to do?

    Any help is appreciated.
    Kent Rancourt
    DevOps Engineer

  2. #2
    Join Date
    Jan 2011
    Location
    Dresden, Germany
    Posts
    525

    Default

    Which version of SDN are you using?

    You should probably use optional relationships:

    Code:
    start user=(%start) match (user)-[?:MADE_DONATION]->() return count(*)
    HTH
    Michael

  3. #3

    Default

    You are right that optional is the key, but your query will never return anything less than one. You should use this instead:

    start user=(%start) match (user)-[r?:MADE_DONATION]->() return count(r)

    "COUNT(<identifier>), ... counts the number of non-null values in <identifier>", while count(*) returns the number of matching subgraphs, which is always at least one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •