[Solved] django.db.utils.ProgrammingError: (1146, u”Table” xxx doesn’t exist”)

1. Delete a table in the database and re-execute Python manage Error in py migrate, prompting that this table does not exist.

2. This is mainly because Django usually creates a new table during the first migration, and the subsequent tables will not create a new table, but only check the changes of fields. Therefore, since we have deleted this table, Django will naturally report an error when checking the changes of fields in this table.

3. The solution is still to execute Python manage Py makemigrations and python manage Py migrate only deletes the record of the table created during the first migration before executing this. Otherwise, it detects that it has been executed for the first time, and the table will not be created later.

(1) In the app module, there is a migrations folder. Except for the first two files, other files are deleted. In fact, every time there is a change, a file will be generated here. The following 001_initial.py is generated during the first migration according to its name. That is, because it exists, no table will be created every time it is executed in the future.

(2) Secondly, there are corresponding records in the database, which should also be deleted. Let’s take a closer look at what is stored in the database. In django_migrations, this table stores records of each migration. Of course, what module is recorded and the corresponding file name. For example, our module here is dtheme, and the file here is 001_initial, which is similar to ours There are one-to-one correspondence in the folder. Similarly, delete this record.

4. Then execute Python manage Py makemigrations and python manage Py migrate is OK. It should be noted that if there are other models under the app module, the tables created by other models should also be deleted, which is equivalent to that our solution is for the whole app module, and all tables will be regenerated if they need to be executed, otherwise some existing errors will be prompted.

Similar Posts: