tags:

views:

53

answers:

1

I'm having major trouble with a get_or_create call. I have it working locally absolutely fine, and the same script is working online on another site. Has anyone got an idea what's going on?

I get "IntegrityError: (1062, "Duplicate entry '2147483647' for key 'PRIMARY'")" whenever I run the script...

for tweet in TwitterSearchFeed.entries:
    if tweet.id[0:3] == 'tag':
        tweet_id = tweet.id.split(':')[2] # tweet.id[tweet.id.rindex(':')+1:] #tag:search.twitter.com,2005:10016544708
        screen_name = tweet.author.split('(')[0].strip()
        text = smart_unicode(tweet.title)
        pub_time = time.strptime(tweet.published, "%Y-%m-%dT%H:%M:%SZ") #2010-01-01T22:20:55Z
        pub_time = datetime.datetime.fromtimestamp(time.mktime(pub_time)) # UTC
        name = tweet.author.split('(')[1].split(')')[0].strip()

        # make sure tweet comes after 30 mins before kickoff
        if pub_time > (game.kickoff - datetime.timedelta(minutes=30)):

            # filter RTs
            if not text[0:3] == "RT ":

                # add or get tweet
                tweet_dict = {
                    'twitter_id': tweet_id,
                    'pub_time': pub_time,
                    'text': text,
                    'name': name,
                    'username': screen_name,
                    'game': game,
                    'atthegame': atthegame
                    }

                obj, created = Tweet.objects.get_or_create(twitter_id=tweet_id, defaults=tweet_dict)

                if created:
                    output += "Tweet added - %s, %s, '%s'<br />" % (tweet_id, screen_name, text)
                else:
                    output += "Tweet %s already exists<br />" % tweet_id

    else:
        output += "Tweet id error<br />" 

return HttpResponse(output)
+2  A: 

My guess is that you suffer from this: http://stackoverflow.com/questions/2235318/how-do-i-deal-with-this-race-condition-in-django/2235624#2235624

Tomasz Zielinski
Yes, that could be it.
Dan