cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Spring data neo4j incorrect node relation issue

I want to model following scenario: User (node) can have multiple posts (node) and Users can follow each other
Here's my spring boot model

@Node("User")
data class NeoUser(
    @Id
    val id: Long,
    val username: String,
    val fullName: String?,
    val isPrivate: Boolean,
    val isVerified: Boolean,
    val profilePicUrl: String?,

    @Relationship(type = "IS_FOLLOWING")
    val friendships: Set<Friendship> = setOf(),
)
@RelationshipProperties()
data class Friendship(
    @Id
    @GeneratedValue
    val id: Long? = null,

    @TargetNode
    val targetNode: NeoUser
)
@Node("Post")
data class NeoPost(
    @Id
    val id: Long,
    val mediaType: MediaType,
    val createdAt: Instant,
    val expireAt: Instant,
    val caption: String?,
    val url: String,
    @Relationship(type = "CREATED_BY", direction = Relationship.Direction.INCOMING)
    val user: NeoUser
)

If i create post for user A, it works fine. However, if user A follows user B, post of user A is also following user B like below image. What am i doing wrong here?

Here's my query if required

MERGE (n:`User` {id: $__id__}) SET n = $__properties__ RETURN id(n)
Instances of class NeoUser with an assigned id will always be treated as new without version property!
MERGE (n:`User` {id: $__id__}) SET n = $__properties__ RETURN id(n)
MATCH (startNode) WHERE startNode.id = $fromId MATCH (endNode) WHERE id(endNode) = 3 MERGE (startNode)-[relProps:`IS_FOLLOWING`]->(endNode) SET relProps = $__properties__
2 REPLIES 2

Could you please provide the version(s) your are using.
I assume that this is a mixture of using the same identifiers for Post and User and due to a problem we had previously where we did not narrow down the node(s) by label.

With the 6.0.3 version of SDN, I get those queries logged.

MERGE (n:`Post` {id: $__id__}) SET n = $__properties__ RETURN id(n);
MERGE (n:`User` {id: $__id__}) SET n = $__properties__ RETURN id(n);
MATCH (startNode:`Post`) WHERE startNode.id = $fromId MATCH (endNode) WHERE id(endNode) = 19 MERGE (startNode)<-[:`CREATED_BY`]-(endNode);
MERGE (n:`User` {id: $__id__}) SET n = $__properties__ RETURN id(n);
MATCH (startNode:`User`) WHERE startNode.id = $fromId MATCH (endNode) WHERE id(endNode) = 20 MERGE (startNode)-[relProps:`IS_FOLLOWING`]->(endNode) SET relProps = $__properties__

Please note esp. the startNode:User in the last line.

I am not sure what was the issue. However, I've removed relationship from data model and creating it manually with repository.