0

I get the following error while trying to run the command python manage.py makemigrations invigilators:

django.db.utils.ProgrammingError: (1146, "Table 'mydatabase.invigilators_shift' doesn't exist")

class Shift(models.Model):
    shiftName = models.CharField(max_length=255,blank=False,unique=True)
    exam = models.ForeignKey(Exam,on_delete=models.CASCADE,related_name="shifts")
    startTime = models.TimeField()
    endTime = models.TimeField()

    def __str__(self):
        return self.shiftName

I have already cleared all previous migrations and an empty database is already created.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
sk_462
  • 587
  • 1
  • 7
  • 16
  • https://stackoverflow.com/questions/27583744/django-table-doesnt-exist – Jagjeet Singh Jan 11 '19 at 09:26
  • 1
    You have code that is trying to query the database before `makemigrations` runs. [Here is a similar question](https://stackoverflow.com/questions/45153674/programmingerror-relation-blah-blah-does-not-exist-trying-to-run-the-specifi). To help with your specific problem, you need to show the full traceback which will show where the query is occuring. – Alasdair Jan 11 '19 at 10:32

4 Answers4

2

Try running these commands

  • python manage.py migrate <app_name> zero
  • delete all initial.py file under app migrations
  • python manage.py makemigrations
  • python manage.py migrate
Anshu Pal
  • 39
  • 3
0

In my case I get the table doesn't exist errors from forms.py. I then comment out the entries in forms.py that refer to a database table. Then I can run makemigrations and migrate. Then uncomment the lines and everything works. This has plagued me for multiple Django releases.

obotezat
  • 1,041
  • 16
  • 20
A. Beckman
  • 1
  • 1
  • 2
0

Below steps solved the problem for me

  1. Delete all migrations files

  2. python manage.py makemigrations (Create your initial migration file 0001_inital.py)

  3. python manage.py migrate --fake <app_name> zero

( Tell Django to mark the migrations as having been applied or unapplied, but without actually running the SQL to change your database schema. This brings the migrations to zeroth state. Otherwise Django will think there's no change to apply )

  1. python manage.py migrate
-1

First Step: Just "Cut" The all models from Models.py & paste that models to the any other text file or notepad.

Second Step: Just "Cut" the all forms from forms.py & paste at the the same text file at you pasted the Models.

Third Step: Make a Comment of all imported models & forms in views.py Because we don't want errors.

Fourth Step: After these three steps you have to just run the command "python manage.py migrate".

Fifth Step: After that command You have to just bring back all the models in models.py and all the forms in forms.py & uncomment all the imported forms and models of views.py.

Last Step: After these all steps you have to just run another two commands 1."python manage.py makemigrations" 2."python manage.py migrate"

& Done,It works.

  • Please avoid such detailed list of steps: "copy", "cut" and "comment" are not useful. Your list cannot be generalized as a good procedure as it is very close to your specific case and very vague "Make a Comment [...] Because we don't want errors". Be simple and provide examples. Hope that my comment can help you :) – Dos Aug 05 '21 at 10:42