简体   繁体   中英

How do I re-create databases after they were deleted?

I used the python manage.py makemigrations myApp and python manage.py migrate myApp commands to create Postgres tables in my database. Then someone manually deleted them with SQL queries.

Can I re-create these tables, without data, using terminal commands?

It depends.

Did they delete all of the tables? If so thats easy. Just rerun the

manage.py migrate. 

Did they only delete new tables that you have added with migrations? Not so bad, go into the django_migrations table, and delete the entries for the migrations that created the deleted tables from that table and rerun

manage.py migrate. 

When you run makemigrations, Django creates the migration file but doesn't do anything with it in terms of touching the database.

When you run the migrate command, Django runs the alter table commands on the database and adds an entry to the django_migrations table, so it knows which migrations have been done, and which still need to be run.

If the person has deleted a random set of tables that are out of sync with your migrations its going to be a mess and its going to involve a lot of manual work to make sure your migrations and database tables are in sync.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM