0

I want to create simple chat system, my models:

class Conversation(models.Model):
    users = models.ManyToManyField(User, related_name='users')
    date = models.DateTimeField(auto_now_add=True)


class Message(models.Model):
    user = models.ForeignKey(User)
    conversation = models.ForeignKey(Conversation, related_name='conversation')
    content = models.TextField()
    date = models.DateTimeField(auto_now_add=True)

and my view:

def conversation(request, username):
    recipient = User.objects.get(username=username)
    conversation, created = Conversation.objects.get_or_create(
        users__in=[recipient, request.user]
    )

    if created:
        conversation.users.add([recipient, request.user])

I want create users connection by create conversation with manytomany field. When i go to /conversations/user_name get_or_create check if conversation exists and if not then create new conversation with current logged user and user from url.

My problem is:

MultipleObjectsReturned at /conversations/user_name
get() returned more than one Conversation -- it returned 2!

How can i solve it? Probably its problem with this manytomany field.. How i can limit users__id lookup to 1?

Random User
  • 73
  • 1
  • 9
user3501587
  • 425
  • 5
  • 16
  • The conversation already exists and there are two objects of Conversations... That's why the error occurred... – zaidfazil Jun 04 '17 at 17:04

1 Answers1

1

get_or_create isn't suitable for this.

This method is atomic assuming correct usage, correct database configuration, and correct behavior of the underlying database. However, if uniqueness is not enforced at the database level for the kwargs used in a get_or_create call (see unique or unique_together), this method is prone to a race-condition which can result in multiple rows with the same parameters being inserted simultaneously.

You don't have a unique key in your table. And the nature of your app is such that a unique key cannot be used. At the same time, a chat message very very occasionally getting repeated isn't a big deal. So just go ahead and create the object without bothering to check for existence.

Also note that checking for existing using a select and then doing the create would be futile. The race condition mentioned would kick in and you will still end up with duplicates.

e4c5
  • 52,766
  • 11
  • 101
  • 134