Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
280 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...