2

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
Moun
  • 325
  • 2
  • 16
  • please provide full traceback and views.py file – Manoj Tolagekar Nov 08 '22 at 10:02
  • `desc = list(Prod.objects.all().values_list('description', flat=True).distinct().order_by('channel'))` do you see this line in your code? That's the problem. Never make any query on the module level, queries should always go in a function or a method. (Writing queries there also has the side effect that those options never get updated... – Abdul Aziz Barkat Nov 08 '22 at 11:54
  • 1
    Does this answer your question? [Migration clashes with forms.py](https://stackoverflow.com/questions/39535983/migration-clashes-with-forms-py) – Abdul Aziz Barkat Nov 08 '22 at 11:59

1 Answers1

1

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

Lewis
  • 2,718
  • 1
  • 11
  • 28
  • It works but still get the same error when i do ```python manage.py runserver``` (no such table) – Moun Nov 08 '22 at 10:31
  • Can you verify the table is there in your database directly? – Lewis Nov 08 '22 at 10:32
  • Yes, it exists. With all other tables I used, and all tables can't be found – Moun Nov 08 '22 at 10:39
  • 1
    Although doing this might solve the problem, or atleast change the error it's better to solve the root cause which is that OP is making a query on the module level (In that form specifically) – Abdul Aziz Barkat Nov 08 '22 at 12:02
  • @AbdulAzizBarkat This was answered before that was apparent. – Lewis Nov 09 '22 at 07:24
  • @Lewis the question was last edited on 2022-11-08 10:13:48Z while you answered on same date 10:16:18Z the OP hasn't posted any new information since so I don't know what you mean by "before that was apparent". Although I'd concur that the OP doesn't even have a [mre] (The code they share won't reproduce the problem), my duplicate closure (and probably even your answer) basically comes from inferring the situation from the stack trace. The basic point of my previous comment is this answer really doesn't solve anything properly OP's going to get the same error on the next migration. – Abdul Aziz Barkat Nov 09 '22 at 07:42