Home > database >  how to resolve eror - "django.db.utils.ProgrammingError"
how to resolve eror - "django.db.utils.ProgrammingError"

Time:10-31

'

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 "/Users/mihirshah/Desktop/api/create_area_api/urls.py", line 3, in <module>
    from create_area_api.views import api_create_area
  File "/Users/mihirshah/Desktop/api/create_area_api/views.py", line 7, in <module>
    class api_create_area(viewsets.ModelViewSet):
  File "/Users/mihirshah/Desktop/api/create_area_api/views.py", line 10, in api_create_area
    print(queryset.get())
  File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 646, in get
    num = len(clone)
  File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 376, in __len__

    self._fetch_all()
  File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 1866, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 87, in __iter__
    results = compiler.execute_sql(
  File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1398, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 103, in execute
    return super().execute(sql, params)
  File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "accounts_project" does not exist
LINE 1: ...."start_date", "accounts_project"."end_date" FROM "accounts_...

`

I typed cmd - "python3 manage.py makemigrations" and encountered the above error,

  • I tried several commands and tried refreshing the database too by changing it,
  • Deleted all the .pyc in migrations and pycache folder but still getting the same problem.
python3 manage.py makemigrations

CodePudding user response:

If you are on new project, without data in database. You can just remove all py files (and not just pyc files) and use makemigrations again

CodePudding user response:

The problem is line 10 of api/create_area_api/views.py, where you do print(queryset.get()).

Because this line isn’t in a method, it is running when the module is loaded. If the table doesn’t exist, this gives an error, which stops you from running makemigrations and migrate.

If you remove the print line then hopefully makemigrations will run.

If you deleted your .py files in the migrations folder along with the .pyc files, then your migrations and database might be out of sync. This can be very difficult to fix. If it's a test project and the data isn't important, the easiest thing to do is to delete the db.sqlite3 database file, and then Django will create a new database when you migrate.

  • Related