I'm having an error UNIQUE constraint failed: seleniumapp_comment.id

1,311 views
Skip to first unread message

karlna...@gmail.com

unread,
Dec 5, 2017, 7:39:33 AM12/5/17
to Django users
I'm currently doing a web scraper app using selenium. It is supposed to scrape several reviews, but it won't proceed because of the error.  Any help would be greatly appreciated.

Error:
UNIQUE constraint failed: seleniumapp_comment.id

models.py:
class ReviewWebsite(models.Model):
website_name = models.CharField(max_length=50)
website_url = models.TextField()

def __str__(self):
return self.website_name + " " + self.website_url


class Comment(models.Model):
website = models.ForeignKey(ReviewWebsite, on_delete=models.CASCADE)
comment_title = models.TextField()
comment_content = models.TextField()
comment_score = models.CharField(max_length=7)
comment_date = models.CharField(max_length=50)


snippet of the external python script:
def insert_to_db(website_name, title, content, score, review_date):

if website_name == "agoda":
site_id = 1
elif website_name == "tripadvisor":
site_id = 2
else:
site_id = 3

website_id = ReviewWebsite.objects.only('id').get(id=site_id)
comment = Comment.objects.create(comment_title=title, comment_content=content, comment_score=score
, comment_date=review_date, website=website_id)

comment.save(force_insert=True)


Output:
===================AGODA PAGE NUMBER: 1
Review: 1
    Title: Great food and great view!!!”
    Comment: Great food and great view!!! We will definitely come back!!!
    Score: 8.0
    Date: December 03, 2017
    Website: agoda
UNIQUE constraint failed: seleniumapp_comment.id


C. Kirby

unread,
Dec 5, 2017, 9:27:31 AM12/5/17
to Django users
You are trying to save the object twice, and telling it to force the second time, causing the error. objects.create() implicitly calls .save() When you then try save(force_insert=True) it hits a unique error.
If you want to have the comment that is being created/saved for later use you should do:

    comment, created = Comment.objects.get_or_create(comment_title=title, comment_content=content, comment_score=score
, comment_date=review_date, website=website_id)

comment will be your comment and created will be a boolean - True if the comment was created, false if it was already in the db and was just fetched.

Kirby
Reply all
Reply to author
Forward
0 new messages