Django migrate when model was previously deleted - django

When I do python manage.py migrate the desired model doesn't appear in my postgres database. I previously deleted all the tables and then migrated it again and it worked but I don't want that because I have loaded many data in the tables that I will have to load again. How to migrate only a certain model.

You need to delete the migration files in app/migrations first and then run makemigrations before running migrate:
python manage.py makemigration
Also make sure to delete the corresponding lines in your django_migrations table.

Related

why django can not create a table after deleting the table and the migration files of the app?

I am working with Django v4.* which I connected it to Postgres DB on the localhost,
I have created my model (Article) then makemigrations then migrate
then I have changed the model by adding extra field, as a result it didn't take effect so I have deleted the table and all the migrations files in articles/migrations folder apart of the __init__.py file, then I did makemigrations then migrate it create a new file 0001_initial.py but its not creating a new table into the DB, unless I drop the whole DB, which is not ideal in the production env!
I am wondering why Django is unable to create the table back again? and how I can get it created as a new table?
Roll back to your initial migration by running below command:
python manage.py migrate --fake <appname> 0001
Followed by:
python manage.py migrate <appname>
Just delete your database with
python manage.py flush
And delete migrations files
You may have mistyped the directory in the terminal

Django migration delete and update

I have already had some migration files, and I made some changes in the model and did
python manage.py makemigrations
python manage.py migrate
After that in postgresql table django_migrations there is a row indicating I've applied that migration, let's call this migrationA.
I deleted the new generated migration file (migrationA), modified a small piece in my model and then did
python manage.py makemigrations
python manage.py migrate
This generate migrationB. I was hoping this can do the same as squashing migration files.
Will this kind of flow cause any trouble? I didn't see any trouble now but want to make sure this is a safe way to do things. In addition, is there any way to revert postgresql to the time before I applied migrationA?
Yes, it will cause trouble. All the migrations are stored in the migrations table and just deleting a migration will produce inconsistencies between your actual migrations and what's recorded.
Before deleting a migration and creating a new one, you need to first revert it by running ./manage.py migrate my_app number_previous_migration_name.

SQL database. I dropped a table, how can I now create it?

To drop table I used that command:
python manage.py dbshell
.tables
DROP TABLE table_what_i_drop;
Then I tried:
python manage.py makemigrations
and:
python manage.py migrate
but the table wasn't created.
I deleted migrations folders from all apps and the dbsqlite3 file and tried again makemigrations and migrate, but databases wasn't created. Now when I tried:
python manage.py dbshell
.tables
there are no tables for any of my apps. I know that losing tables is my fault, but how I can make all databases from the beginning?
There are two steps in migrations - make migrations and migrate.
makemigrations creates the migration file based on database changes.
migrate runs the migrations and makes the database changes. It also logs what migrations have been run in the table:
django_migrations
You deleted the migrations folder,but there will still be an entry in django migrations saying that your previous migrations have run.
So to fix this, go to the django_migrations table and remove the entries related to your app (there will also be entries for Django tables, such as auth and contenttypes, so don't delete those)
(Alternately just delete the whole sqllite database and start from scratch if you don't mind loosing data in the database)

What is the command to force all migrations in Django?

I switched database names, and now my Django models are out of sync with my database tables. What is the command to force Django to perform all migrations to sync up the models and the tables? I don't want manage.py migrate --fake.
I fixed this by manually deleting all the migrations and running makemigrations again to get a new initial migration file. Then, I went into my database and manually dropped all the tables that Django created in the app. Finally, I deleted all of the rows in the table django.migrations that included the app name. After all that, I ran manage.py migrate and the database was in sync.
When you change something in your models you must execute:
python manage.py makemigrations
for creation new changed schema of your models. Then:
python manage.py migrate <app_name>

Deleted migration files- how to makemigrations without losing data

I changed a field on a model class (which has no classes point to it, only one foreign key pointing out of it). Somehow, this stuffed up my migrations and it keeps saying "django.db.migrations.graph.NodeNotFoundError:" looking for migration files that do not exist.
I accidentally deleted several files in my 'migrations' folder.
My database contains a lot of data, and I do not want to break it.
Will I lose any data if I:
Remove the table that caused the problem in the first place (psql, \d, DROP TABLE tablename)
delete all my migration files
Re run the migration from the start?
./manage.py makemigrations
./manage.py migrate
Can anyone recommend another way of fixing this?
Here is the traceback:
http://dpaste.com/0Y1YDXS
Aren't you using git so that you can get your migration files back? If not, install and use it, starting now
I would suggest:
make a backup/dump of your database first, in case something goes wrong
Delete all migrations
Empty migration table in psql
call makemigrations
call migrate --fake-initial
Empty the django_migrations table:
delete from django_migrations;
Remove all the files in migrations folders in each and every app of your project.
Reset the migrations for the "built-in" apps:
python manage.py migrate --fake
Create initial migrations for each and every app:
python manage.py makemigrations your_app_name
Final step is to create fake initial migrations:
python manage.py migrate --fake-initial
See here https://micropyramid.com/blog/how-to-create-initial-django-migrations-for-existing-schema/