Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

Django conversations - get_or_create MultipleObjectsReturned

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...