title: Database design for upvote/downvote in discussion forum?
body: <p>I have a webite where there is a discussion forum. All questions have unique ID and answers have also unique ID with a foreign key referencing to question ID.</p>
  
  <p>I have a user logged in. Using ajax, I can send the data(upvote/downvote,QID/AID, username).But there are few problems.</p>
  
  <ol>
  <li><p>Whenever user logs back in, I want that when he opens a question, his previous upvotes and downvotes should be displayed.</p></li>
  <li><p>A user cannot upvote/downvote a same question multiple times. Like if he upvoted a question to +1, he can only downvote it, not upvote it again.</p></li>
  </ol>
  
  <p>Solution according to me. I am thinking of maintaining a table where username is the primary key and another attribute is a list of all the upvotes and downvotes he has done. Example:</p>
  
  <pre><code>(username, array)
  (baqir, up_A001 up_A050 down_Q011 up_Q123)
  </code></pre>
  
  <p>Whenever the user logs in, I take this array and make sure all the previous upvotes and downvotes of the user are as it is. If there is a new vote,I add it to the array.</p>
  
  <p>Is there any better solution?</p>
  
  <p>P.S. Not necceassary but my current database structure.</p>
  
  <pre><code>class Question(models.Model):
      QID = models.CharField( default="",max_length=1000, primary_key=True)
      title = models.CharField(max_length=30, default="")
      description = models.CharField(max_length=1055, default="")
      date = models.DateTimeField(default=datetime.now, blank=True)
      username = models.CharField(max_length=6, default="")
      votes = models.IntegerField(default=0)
      approved = models.BooleanField(default=False)
  
      def __str__(self):
          return self.title
  
  class Answer(models.Model):
      AID = models.CharField(max_length=1000, default="", primary_key=True)
      QID = models.ForeignKey(Question,on_delete=None)
      description = models.CharField(max_length=1055, default="")
      date = models.DateTimeField(default=datetime.now, blank=True)
      username = models.CharField(max_length=6, default="")
      votes = models.IntegerField(default=0)
      approved = models.BooleanField(default=False)
  
      def __str__(self):
          return self.AID
  </code></pre>
  
  <p>I am using Django. And I basically want whatever stackoverflow has implemented for their voting of questions and answers.</p>