<p dir="auto">I have reproduced this on 1.8.1 and 1.8.2.</p>
<p dir="auto">Create a node that has 1 link to itself.  The query below is designed to return the node and its relationships separated into incoming and outgoing relationships. It also includes the start nodes (for incoming relationships) and end nodes (for outgoing relationships).</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="START n=node(12) MATCH n&lt;-[links_n_in?]-(n_from),n-[links_n_out?]-&gt;(n_to) RETURN n,COLLECT(n_from) AS n_from,COLLECT(links_n_in) AS links_n_in,COLLECT(n_to) AS n_to,COLLECT(links_n_out) AS links_n_out"><pre class="notranslate"><code class="notranslate">START n=node(12) MATCH n&lt;-[links_n_in?]-(n_from),n-[links_n_out?]-&gt;(n_to) RETURN n,COLLECT(n_from) AS n_from,COLLECT(links_n_in) AS links_n_in,COLLECT(n_to) AS n_to,COLLECT(links_n_out) AS links_n_out
</code></pre></div>
<p dir="auto">When the node has 1 self-referential relationship, either the links_n_in or links_n_out array is populated with the expected relationships, but not both.  And the behavior seems to be nondeterministic.  Sometimes the links_n_in array is populated and sometimes the links_n_out array is populated.  Note the use of optional relationships in the query. When I do not use optional relationships (omit the '?'), this behavior does not occur and both arrays are populated as expected.</p>
<p dir="auto">When the node has 2 self-referential relationships, this problem does not occur and both arrays are populated with 2 relationships as expected. This is true regardless of whether optional relationships are used in the query.</p>
<p dir="auto">However, when the node has 3 self-referential relationships, both arrays are populated with 6 relationships instead of the expected 3.  In other words, each relationship is duplicated.  Similar behavior also results with more than 3 relationships.  This occurs regardless of whether optional relationships are used in the query. Note that adding DISTINCT inside COLLECT(links_n_in) and COLLECT(links_n_out) fixes this problem.</p>
<p dir="auto">I have created a neo4j DB locally and assigned some indices and unique constraint.<br>
Here is the :schema<br>
Indexes<br>
ON :Actor(social_id) ONLINE (for uniqueness constraint)<br>
ON :Category(name)   ONLINE (for uniqueness constraint)<br>
Constraints<br>
ON (actor:Actor) ASSERT actor.social_id IS UNIQUE<br>
ON (category:Category) ASSERT category.name IS UNIQUE<br>
Additionally I do a createIfNotFound() in my java code, where I first do a find and then create if not found.</p>
<p dir="auto">Inspite of these two checks I still see multiple nodes of Label category and same name existing in the DB.</p>
<p dir="auto">MATCH (n:Category) WHERE n.name='garden' RETURN n<br>
name    garden<br>
name    garden<br>
name    garden<br>
name    garden<br>
Returned 4 rows in 49 ms.</p>
<p dir="auto">After a lot of debugging and restarting the server, I see a different error for another node which should be unique.</p>
<p dir="auto">MATCH (n:Category) where n.name='garden' RETURN n;</p>
<p dir="auto">More than one item in org.neo4j.helpers.collection.IteratorUtil$12@20c98f24, first:2, second:450</p>
<p dir="auto">Am i going wrong somewhere conceptually? Please help.</p>