Home > Mobile >  django.db.utils.OperationalError: no such table: product, django cannot import form.py into view.py
django.db.utils.OperationalError: no such table: product, django cannot import form.py into view.py

Time:11-09

I've 3 pages Model, Forms and View

In model.py i define my table

#model.py  

class Prod(models.Model):
    id = models.CharField(primary_key=True, max_length=10, blank=False, null=False)
    description = models.TextField(blank=True, null=True)
    
    
    class Meta:
        managed = False
        db_table = 'product'
        verbose_name = 'PROD'

now I move to forms.py

#forms.py

from .models import Prod

class ProdForm(forms.ModelForm):
    #some code to fill

    class Meta:
        model = Prod
        exclude = [...]
        widgets = {
            ...
            }
        labels = {
            ...
        }

and finally with view.py

#view.py

from .forms import ProdForm

When i execute python manage.py makemigrations, I get an error saying

no such table: product

my app was hosting locally with postgres, now i used db.sqlite3 to test, i don't undestand why views.py can't import the table forms or at least why It's not creating them?

I don't understand why, specially that this code was running locally with a postgres.

traceback:

    Traceback (most recent call last):
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: product

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

Traceback (most recent call last):
  File "C:\TR v2 arz\alpha_arz_apps\manage.py", line 22, in <module>
    main()
  File "C:\TR v2 arz\alpha_arz_apps\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\management\base.py", line 393, in execute
    self.check()
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\management\base.py", line 419, in check
    all_issues = checks.run_checks(
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\APP\envs\.webdev\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\APP\envs\.webdev\lib\site-packages\django\urls\resolvers.py", line 412, in check
    for pattern in self.url_patterns:
  File "C:\APP\envs\.webdev\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\APP\envs\.webdev\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\APP\envs\.webdev\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\APP\envs\.webdev\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\amq.o\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\TR v2 arz\alpha_arz_apps\alpha_arz_apps\urls.py", line 23, in <module>
    path('TR/', include('TR.urls')),
  File "C:\APP\envs\.webdev\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\amq.o\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\TR v2 arz\alpha_arz_apps\TR\urls.py", line 3, in <module>
    import TR.views as views
  File "C:\TR v2 arz\alpha_arz_apps\TR\views.py", line 14, in <module>
    from .forms import ProdForm
  File "C:\TR v2 arz\alpha_arz_apps\TR\forms.py", line 50, in <module>
    class ProdForm(forms.ModelForm):
  File "C:\TR v2 arz\alpha_arz_apps\TR\forms.py", line 55, in ProdForm
    desc = list(Prod.objects.all().values_list('description', flat=True).distinct().order_by('channel'))
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\models\query.py", line 262, in __len__
    self._fetch_all()
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\models\query.py", line 171, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\models\sql\compiler.py", line 1130, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\backends\utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\APP\envs\.webdev\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: product

CodePudding user response:

Remove the ProdForm definition from forms.py

Call python manage.py makemigrations

Call python manage.py migrate

Add form back into forms.py

Note: Always migrate your new model definitions before using them as a dependency

CodePudding user response:

Simply you can delete your all migrations folder and migrate manually using below command:

python manage.py makemigrations appname

python manage.py sqlmigrate appname 0001

python manage.py migrate

Try above things and see if it solves your error

  • Related