Announcement Announcement Module
Collapse
No announcement yet.
Issue counting nodes or relationships when count is 0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

  • #2
    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

    Comment


    • #3
      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.

      Comment

      Working...
      X