35

There are two models Groups and Students and only one table for Groups of them, the Students table was deleted.

How to make Django recreate the deleted table? If I do makemigrations it prints "No changes detected".

On admin page when I click on the Students table it throws an exception:

relation "students_students" does not exist
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Егор Лебедев
  • 1,161
  • 1
  • 10
  • 26
  • have you tried `syncdb` ? – Vaulstein Oct 21 '15 at 12:37
  • Yes, it throws error: students_groups already exist, but nothing about students_students – Егор Лебедев Oct 21 '15 at 12:40
  • Why would you want Django to create it? If database schema was changed manually, then it should be restored manually as well. The answer is SQL. – Ernest Oct 21 '15 at 12:55
  • its a work around, make small changes in students model and generate new migration file for the students changes using makemigrations and then run migrate command.. its should create students table. – Shreeyansh Jain Oct 21 '15 at 18:46
  • **WARNING**: Many of the answers below involve deleting the migrations / faking the migrations, etc. these solutions are generally inadvisable unless you know **exactly** what you are doing, the safest answers would be the one's suggesting to use `sqlmigrate` to get the SQL required to create the table. – Abdul Aziz Barkat Nov 01 '22 at 05:05

16 Answers16

90

In django 1.7 you can try:

1. Delete your migrations folder

2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.
   You could alternatively just truncate this table.

3. python manage.py makemigrations

4. python manage.py migrate --fake

If you are working in django 1.9.5 this is the 100 % solution for this problem:

1. Delete your migrations folder

2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.
   You could alternatively just truncate this table.

3. python manage.py makemigrations app_name

4. python manage.py migrate

This works 100% for me!

Raúl EL
  • 1,206
  • 11
  • 6
22

Jan 2021

I had a migration problem and I had to drop/delete a table by pgadmin. Then, when I makemigrations and migrate the table wasn't recreated. In this way, I've found this procedure which worked for me:

python manage.py migrate --fake app_name zero 
python manage.py migrate app_name

[NOTE]

  • If you don't have the intended migration file, create that before the above commands by python manage.py makemigrations
  • If you don't want to roll back to the initial(zero) state use the number of migration file instead of zero, e.g. python manage.py migrate --fake myappname 0005
  • I tested this approach in Django 2.2

Read More

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • Be careful, this might try recreate other models in the app as well, though they are already in the db. – flgn Nov 16 '21 at 10:31
  • worked for me. Thnaks - In my case, I dropped the table by mistake from postgres manually. – Aziz Jul 21 '22 at 20:59
18

There isn't an easy way to get Django to recreate a table that you have deleted manually. Once your database is altered manually, Django's view of the database (from migrations) is different from reality, and it can be tricky to fix.

If you run the sqlmigrate command, it will show you the required SQL to create the table. You can run the sql in a database shell. Assuming your app name is students, and the migration that created the table was 00XX_create_students.py, you would do:

./manage.py sqlmigrate students 00XX_create_students

Be careful if there are foreign keys to or from the students table, the constraints will have to be created as well.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 2
    Correct, although in this case he would probably need to run `sqlmigrate` instead – gbs Oct 21 '15 at 12:44
  • with `sqlall` prints `CommandError: App students hasmigrations. Only the sqlmigrate and sqlflush can be used`, with `sqlmigrate` there requared migration name, there can I find it? – Егор Лебедев Oct 21 '15 at 12:52
  • @gbs good point, I've changed the answer to use `sqlmigrate` instead. – Alasdair Oct 21 '15 at 12:58
  • You'll need to search your `students/migrations` directory to find the migration that created the model. Search for the model name `Student`. Be careful if there were any subsequent migrations that altered the table (e.g. added or removed a field), because you'll have to run the appropriate SQL from those migrations as well. – Alasdair Oct 21 '15 at 13:00
  • Best tip. Just be sure to run this with the settings pointing database config of the DB you are targeting, otherwise it might produce invalid SQL. – karuhanga Dec 26 '18 at 21:19
16

The only way that worked for me:

rm -r <app-name>/migrations/
python manage.py makemigrations <app-name>
python manage.py sqlmigrate <app-name> 0001_initial

Copy what it prints out (or, depending on what you have actually removed from the DB, only part of the SQL queries).

Apply those copied queries to your DB:

psql -U user_name -h 127.0.0.1 database_name

Paste what you have copied from the SQL queries printout.

Commit the queries.

And that's it - your missing tables are created.

Airstriker
  • 331
  • 2
  • 5
3

The answer that worked for me is as follows:

Assume in your database a table of a model has been deleted and you need to re-create, then do the following.

  • comment out the model in models.py that creates the table that has been deleted (either the model class or a line that creates a table like a = models.ManyToManyField(...))

  • run: python manage.py makemigrations <app-name>, where <app-name> is the name of of the app where you have models.py

  • run: python manage.py migrate --fake <app-name>

  • un-comment the model in models.py

  • run: python manage.py makemigrations <app-name>

  • run: python manage.py migrate <app-name> (without the --fake)

and you the table should be back in the database. But any data that was in the table will be lost.

Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29
  • This is actually one of the safer answers posted on this question. To anyone reading this do note that you'll probably also have to comment a bit of your code referring to the model for the deleted table. Also in case there are other people using your app, after you have the table back you should delete these new migrations and fake back to the last migration before those. – Abdul Aziz Barkat Nov 01 '22 at 05:04
2

Delete the migration folder from your migration app folder and simply run the migration commands:

  1. python3 manage.py makemigrations appname
  2. python3 manage.py migrate
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
1

I just deleted my migrations folder, dropped the whole database, then i made migration for the app

python3 manage.py makemigration 
python3 manage.py migrate

and it came back.

1

Rename the deleted table name to some_new_name in the models.py and run:

python3 manage.py makemigration
python3 manage.py migrate

again rename the some_new_name table to the original name and run

python3 manage.py makemigration
python3 manage.py migrate

finally, go to the dbshell and drop the table some_new_name

Programming-Lover
  • 1,177
  • 10
  • 14
0

I create table manualy and it helps.

Егор Лебедев
  • 1,161
  • 1
  • 10
  • 26
0

For Django 1.10.4 I deleted the db.sqlite3 file from the project folder and then ran the following commands:

  1. python manage.py makemigrations app_name
  2. python manage.py migrate
siddharth
  • 190
  • 3
  • 10
0

Django 1.11.2 using MariaDB, accidental drop of database table. To recreate table, try the following: 1/ Delete all except for init.py in your app/migrations directory 2/ select * from django_migrations; delete from django_migrations where app = 'yourapp'; 3/ Check your model is good and run: python manage.py makemigrations 4/ python manage.py migrate

Works for me!

0

if you have created your classes and performed the migration operation, and then you want to add items to your classes, empty the migration folder with this command beforehand. In Django 3, I proceeded according to the following steps and it worked 100%

  1. python manage.py makemigrations appname --empty
  2. python manage.py makemigrations appname
  3. python manage.py migrate
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
0

Actually, the above methods did not work for me, so I just perform the below workaround as I did not want to manually write the whole query to create the table.

So I changed the database in the settings file and re-ran the migrations command after deleting the migrations folder, then just performed the python migrate command it created new tables in another database then from there just opened the table in query view, copied the script, and inserted the table in my main database.

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
simar
  • 1
0

Another Simple way to do this is

  • Go to your migrations folder.
  • Search for the file which contains the code to create the Students table in DB.
  • Remove the code snippet from the file and save it.
  • Then run py manage.py makemigrations and py manage.py migrate again

This worked for me :)

0

In this case, you need to trick django! Do one thing...

  • copy the "students" model in models.py with other name like "studentscopy".
  • Now run --> python manage.py makemigration
  • It will create a new migration in migration package of your app. Open that last migration and rename "studentscopy" back to "students" in that file.
  • Now run --> python manage.py migrate

It will create the table again with "students" name and at last delete that "studentscopy" model from your models.py file.

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