0

i have this problem when i run my testcase with django and mysql.

when i run manage.py test i've got this error.

Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_sensor_list_view (gestioneSensori.tests.ViewsTest.test_sensor_list_view)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
    res = self._query(mogrified_query)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
    db.query(q)
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\MySQLdb\connections.py", line 265, in query
    _mysql.connection.query(self, query)
MySQLdb.ProgrammingError: (1146, "Table 'test_db.sensori' doesn't exist")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\gestioneSensori\tests.py", line 10, in test_sensor_list_view
    sensore1 = Sensori.objects.create(id=1, tipo="Temperature", descrizione='desc', accuratezza=.0, precisione=.0, temperaturamin=0.0, temperaturamax=10.0, tolleranza=.1, tensioneesercizio=12.0, sensibilita=.2)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\query.py", line 679, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\base.py", line 892, in save
    self.save_base(
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\base.py", line 998, in save_base
    updated = self._save_table(
              ^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\base.py", line 1161, in _save_table
    results = self._do_insert(
              ^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\base.py", line 1202, in _do_insert
    return manager._insert(
           ^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\query.py", line 1847, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\models\sql\compiler.py", line 1836, in execute_sql        
    cursor.execute(sql, params)
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\backends\utils.py", line 79, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\backends\utils.py", line 92, in _execute_with_wrappers    
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\backends\utils.py", line 100, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\backends\utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute
    return self.cursor.execute(query, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\MySQLdb\cursors.py", line 179, in execute
    res = self._query(mogrified_query)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\MySQLdb\cursors.py", line 330, in _query
    db.query(q)
  File "C:\Users\faboz\OneDrive\Desktop\ESAME_TWAI\Codice\venv\Lib\site-packages\MySQLdb\connections.py", line 265, in query
    _mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'test_db.sensori' doesn't exist")

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (errors=1)
Destroying test database for alias 'default'...

here is my models.py

from django.db import models


class Sensori(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    tipo = models.CharField(db_column='Tipo', max_length=45, blank=True, null=True)  # Field name made lowercase.
    descrizione = models.CharField(db_column='Descrizione', max_length=255, blank=True, null=True)  # Field name made lowercase.
    accuratezza = models.FloatField(db_column='Accuratezza', blank=True, null=True)  # Field name made lowercase.
    precisione = models.FloatField(db_column='Precisione', blank=True, null=True)  # Field name made lowercase.
    temperaturamin = models.FloatField(db_column='TemperaturaMin', blank=True, null=True)  # Field name made lowercase.
    temperaturamax = models.FloatField(db_column='TemperaturaMax', blank=True, null=True)  # Field name made lowercase.
    tolleranza = models.FloatField(db_column='Tolleranza', blank=True, null=True)  # Field name made lowercase.
    tensioneesercizio = models.FloatField(db_column='TensioneEsercizio', blank=True, null=True)  # Field name made lowercase.
    sensibilita = models.FloatField(db_column='Sensibilita', blank=True, null=True)  # Field name made lowercase.

    class Meta: #database esterno
        managed = False
        db_table = 'Sensori'
        app_label = 'gestioneSensori'

class Letture(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True)  # Field name made lowercase.
    valore = models.FloatField(db_column='Valore', blank=True, null=True)  # Field name made lowercase.
    unita = models.CharField(db_column='Unita', max_length=10, blank=True, null=True)  # Field name made lowercase.
    datarilevazione = models.DateTimeField(auto_now_add=True, db_column='DataRilevazione')  # Field name made lowercase.
    sensoriid = models.ForeignKey(Sensori, db_column='SensoriID', on_delete=models.CASCADE, null=False)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'Letture'
        app_label = 'gestioneSensori'

The only things i didn't tried is to not use mysql, but the default django database. the server run well, but i cannot execute automatic testings could you help me?

i cannot find a solution

1 Answer 1

0

You have managed=False in your Meta options which means no database table creation, modification, or deletion operations will be performed for this model. Simply put, Django is not managing your database.

Set managed=True and run the python manage.py makemigrations command and also python manage.py migrate.

The docs also says:

For tests involving models with managed=False, it’s up to you to ensure the correct tables are created as part of the test setup.

See the docs.

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

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.