title: why I receive 'Duplicate entry' error in django testing?
body: <p>I had some unit tests in Django that worked fine. I added some lines to send queries to MySQL. After that every unit test that includes authentication is raising exceptions, even I remove those lines. what is the problem?</p>
  <h2>Django error report:</h2>
  <blockquote>
  <p>_mysql.connection.query(self, query)
  django.db.utils.IntegrityError: (1062, &quot;Duplicate entry 'myuser' for key 'auth_user.username'&quot;)</p>
  </blockquote>
  <h2>My Tests:</h2>
  <pre><code>class AnnotationPage(TestCase):
      def setUp(self):
          self.factory = RequestFactory()
          self.user = User.objects.create_user(
              username='myuser', password='12345678')
  
  
      def test_labeling(self):
          ajaxData = {'testing': 'True',
                      'direction': 'forward'}
          request = self.factory.post('/annotation', ajaxData)
          request.user = self.user
          response = AnnotationView(request)
          assert response.content == b'{&quot;image&quot;: &quot;image.png&quot;, &quot;id&quot;: 1}'
  
  </code></pre>
  <h2>My Tests with queries:</h2>
  <pre><code>class AnnotationPage(TestCase):
      def setUp(self):
          self.factory = RequestFactory()
          self.user = User.objects.create_user(
              username='myuser', password='12345678')
          self.cursor = connection.cursor()
          query = &quot;TRUNCATE TABLE homepage_imagelabel&quot;
          self.cursor.execute(query)
  
  
      def test_labeling(self):
          self.cursor = connection.cursor()
          query = &quot;INSERT INTO homepage_imagelabel (ImageFile) VALUES ('image.png')&quot;
          self.cursor.execute(query)
          ajaxData = {'testing': 'True',
                      'direction': 'forward'}
          request = self.factory.post('/annotation', ajaxData)
          request.user = self.user
          response = AnnotationView(request)
          assert response.content == b'{&quot;image&quot;: &quot;image.png&quot;, &quot;id&quot;: 1}'
  </code></pre>