2

I use Hasura and I have a social-network like situation. In which I have a "User" object and a "Feed" object. Every user has a feed. I have a relationship from user.id to feed.id.

The relevant mutation is UpsertUserDetails as follows:

  mutation UserDetailsUpsert(
    $email: String!
    $picture: String
  ) {
    insert_users_one(
      object: {
        email: $email
        feed: { data: {} }
        picture: $picture
      }
      on_conflict: { constraint: users_tid_email_key, update_columns: [picture] }
    ) {
      id
    }
  }

So when I create a new user it also creates a feed for it.

But when I only update user details I don't want it to create a new feed. I would like to stop the upsert from going through to relationships instead of the above default behavior.

and according to this manual I don't see if its even possible: https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/upsert.html#upsert-in-nested-mutations

To allow upserting in nested cases, set update_columns: []. By doing this, in case of a conflict, the conflicted column/s will be updated with the new value (which is the same values as they had before and hence will effectively leave them unchanged) and will allow the upsert to go through.

Thanks!

fokolo
  • 33
  • 3
  • Is there ever a case where a user should be able to have more than one feed? Have you considered putting a unique constraint on the `user_id` column inside the feed table to prevent this from happening? – Jesse Carter Oct 13 '21 at 14:29
  • This is a problem for my case because feeds can be also for `Groups` so the generic ID field needs to be constrained on two tables. Is it possible? – fokolo Oct 13 '21 at 16:19
  • 1
    Hey did you find any solution for it, I am stuck with the same issue. – Lakshya Sharma Mar 08 '22 at 18:14

1 Answers1

0

I'd recommend that you design your schema such that bad data cannot be entered in the first place. You can put partial unique indices on the feed table in order to prevent duplicate feeds from ever being created. Since you have both users and groups you can implement it with 2 partial indices.

CREATE UNIQUE INDEX unique_feed_per_user ON feed (user_id)
    WHERE user_id IS NOT NULL;

CREATE UNIQUE INDEX unique_feed_per_group ON feed (group_id)
    WHERE group_id IS NOT NULL;
Jesse Carter
  • 20,062
  • 7
  • 64
  • 101