3

I'm running a simple test case in Django, I have a model Subscribers but when I run python manage.py test It gives me the following error

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_subscriber_fullname 
(gatpulsecore.tests.test_models.SubscribersTest)
Test method get_fullname
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 170, in execute
    result = self._query(query)
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 328, in _query
    conn.query(q)
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result
    result.read()
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 683, in _read_packet
    packet.check_error()
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\dania\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1146, "Table 'test_gatpulsedevinstance.subscribers' doesn't exist")

My model looks like this

from django.db import models

class Subscribers(models.Model):
    """ Subscriber Model """   
    idsubscribers = models.AutoField(primary_key=True)
    legalid = models.CharField(max_length=45, blank=True, null=True)
    name = models.CharField(max_length=45, blank=True, null=True)
    lastname = models.CharField(max_length=45, blank=True, null=True)
    initialdate = models.DateField(blank=True, null=True)
    bday = models.DateField(blank=True, null=True)
    email = models.CharField(max_length=100, blank=True, null=True)
    phone = models.CharField(max_length=45, blank=True, null=True)
    emergencyphone = models.CharField(max_length=45, blank=True, null=True)
    photolink = models.CharField(max_length=200, blank=True, null=True)
    medicalconditions = models.CharField(max_length=200, blank=True, null=True)
    objectives = models.CharField(max_length=200, blank=True, null=True)
    paymentfrequency = models.CharField(max_length=45, blank=True, null=True)

    def get_fullname(self):
        return self.name + " " + self.lastname
    class Meta:
        managed = False
        db_table = 'subscribers'

And my test file

from django.test import TransactionTestCase
from gatpulsecore.models import Subscribers

class SubscribersTest(TransactionTestCase):
    """ Test module for Subscribers model """

    def setUp(self):
        Subscribers.objects.create(
            legalid='34214555', name='Casper', lastname='Smith')
        Subscribers.objects.create(
            legalid='24612555', name='John', lastname='Rogers')

    def test_subscriber_fullname(self):
        """ Test method get_fullname """
        sub_casper = Subscribers.objects.get(name='Casper')
        sub_john = Subscribers.objects.get(name='John')
        self.assertEqual(
            sub_casper.get_fullname(), "Casper Smith")
        self.assertEqual(
            sub_john.get_fullname(), "John Rogers")

I already tried migrating but it didn't help, also someone recommended to TransactionTestCase instead of TestCase but it didn't work either. Does anybody has encountered with the same error? I've been searching for a workaround but I don't seem to find anything helpful.

2 Answers 2

0

That can happen if you are using more than one database, in such case you have to specify the database by using:

MyModel.objects.using("database_name")

Or simply specify that particular database as the main one.

From what I understand, django is not detecting your db or you have more than one.

Sign up to request clarification or add additional context in comments.

1 Comment

I see, the weird part is that I only have one database registered in the settings.py and specifying the database did not work, either ways thanks
0

Once try explicitly specifying the test database name in settings.py, e.g.,

DATABASES = {
    'default': {
        ...
        'TEST': {
            # Avoid naming it the same as your original database name. 
            # Otherwise, it will be used while testing (and even may get deleted).
            'NAME': 'name_of_your_choice',
        },
    },
    ...
}

If you are using multiple databases, you may need to do it for all of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.