prisma / prisma1

💾 Database Tools incl. ORM, Migrations and Admin UI (Postgres, MySQL & MongoDB) [deprecated]
https://v1.prisma.io/docs/
Apache License 2.0
16.55k stars 863 forks source link

Disconnect in a many-to-many relationship removes unrelated relations #2947

Closed ValCanBuild closed 6 years ago

ValCanBuild commented 6 years ago

Suppose my data model is such:

type User {
    id: ID! @unique
    email: String! @unique

    # a list of people the user follows
    follows: [User!]! @relation(name: "Follows")
    # a list of people following this user
    followers: [User!]! @relation(name: "Following")
}

So a user can follow other users and can have users follow them. Pretty simple. Our starting state for 3 users is:

screen shot 2018-08-20 at 14 26 03

Now, suppose my user (top one) follows the other two using the following code:

    async userFollow(loggedInUserId: string, userToFollowId: ID_Input, info?: GraphQLResolveInfo | string): Promise<User> {
        await this.db.mutation.updateUser({
            where: {id: loggedInUserId}, data: {
                follows: {
                    connect: [{id : userToFollowId}]
                }
            }
        }, "{ id }");

        return this.db.mutation.updateUser({
            where: {id: userToFollowId}, data: {
                followers: {
                    connect: [{id: loggedInUserId}]
                }
            }
        }, info);
    }

The result would be as you'd expect: screen shot 2018-08-20 at 14 31 00

Now, here is where it goes wrong. I unfollow user Jane using the following code:

    async userUnfollow(loggedInUserId: string, userToUnfollowId: ID_Input, info?: GraphQLResolveInfo | string): Promise<User> {
        await this.db.mutation.updateUser({
            where: {id: loggedInUserId}, data: {
                follows: {
                    disconnect: [{id : userToUnfollowId}]
                }
            }
        }, "{ id }");

        return this.db.mutation.updateUser({
            where: {id: userToUnfollowId}, data: {
                followers: {
                    disconnect: [{id: loggedInUserId}]
                }
            }
        }, info);
    }

And the output is: screen shot 2018-08-20 at 14 32 57

As you can see, unfollowing one user has resulted in the other user's followers connection to also be removed, while the follows connection of the user who initiated the action remains correct resulting in an invalid state.

Expected behavior I'd expect the followers connection of the unrelated user in this transaction (John) to be unchanged.

Versions (please complete the following information): Prisma version: 1.14.1 Prisma-binding version: 2.1.4 GraphQL version: 0.13.2 GraphQL Yoga version: 1.15.1

Additional information This bug looks like https://github.com/prisma/prisma/issues/2810 but I should be on a version of Prisma that's already fixed it.

ValCanBuild commented 6 years ago

Closing as I discovered it was due to not updating the local docker image to the bug fix version of #2810