I use django-admin startproject to create a new project, then use python manage.py migrate.
I find there are some tables will create by default.
I want migrate my tables only when I run python manage.py migrate. Is it possible to skip the default?
By default, INSTALLED_APPS contains django.contrib.admin and django.contrib.auth apps (along with few other apps). These apps are part of Django. These apps are optional, and can be removed if you do not need functionality they provide (See: Run django application without django.contrib.admin).
python manage.py makemigrations will create the migrations files for all the changes to models in all the apps.
And python manage.py migrate will apply previously computed migrations to the database.
When you have multiple apps, use python manage.py makemigrations [app_name], and python manage.py migrate [app_name] to limit these operations to specific app.
Related
If I use mongodb in django project with the help of djongo third-party api should I have to use commands migrate and makemigrations again n again when ever I make changes in my models??
I am new to publishing django apps on heroku.
I have read their tutorials and figured I would start simple by modifying their template django project.
So far everything was good.
Then I made an app as one does, made a model and ran
python3 manage.py makemigrations my_app
python3 manage.py migrate
and things seemed ok.
I then pushed to heroku and there were no immediate complaints.
However, now when I try to make save a new model I get the error:
ProgrammingError at /my_app/
relation "my_app_myappmodel" does not exist
LINE 1: INSERT INTO "my_app_myappmodel" ("field1", "field2", "field3") VALUES...
odd...
So I run this locally and everything works fine.
I have tried cleaning my migrations, faking my migrations, squashing my migrations, etc (as other S.O. posts suggest)
Nothing works.
What is up and how do I fix it?
You need to actually run the migrations on Heroku once you have pushed the code generated by makemigrations. You do this via heroku run manage.py migrate.
run the following command from your terminal
heroku run python manage.py migrate
or you can also do:
in your local settings.py, change your DATABASES variable to use the heroku one then run from the terminal
python manage.py makemigrations
python manage.py migrate
but you should not normally locally make changes to the heroku production database (as in option 2) except if you are really desperate or don't care
My all SQL schema was deleted cause of windows10 initialization.
So, I wanna run makemigrations with already existing django project code(with so many lines......).
But "python manage.py makemigrations" not works with error 'Table doesn't exist'... Is there some methods overcome these situations ???
Your existing migrations scripts are corrupt. So you need to do the following steps:
Delete the migrations folder inside your app
Then run makemigrations
python manage.py makemigrations
Apply the changes to DB
python manage.py migrate
I have one model with the relevant fields setup. I ran manage.py migrate then it created the a table in my database. I then ran manage.py makemigrations so that it could create an initial migration file with prefix '0001'. I added another field to my model and then ran manage.py makemigrations again, which created another migration file with prefix '0002'. When I run manage.py migrate now it still looks at the first migration file and so throws out an error that the 'Table already exists'. How can I make it only look at the latest migration file so that it adds the new column to the table? I'm using MySQL.
This will never happen unless django detects (thinks) that the database has not been setup, and tries to initialise the tables with a schema.
Looks like your tables are already setup, but this is not known to django. That is why it tries to start applying the first migration - the table creation and schema is included in that.
You can use the --fake and --fake-initial options as per your specific problem, which tell django that the tables are already setup and ready, and to fake the migrations.
Useful links for more info:
django migrations - django documentation
django migrations, a primer - realpython
how to redo a migration after fake - stackoverflow
I have a django project with migrated and unmigrated apps. I can be able to selectively migrate the migrated applications by running the command :
migrate <app> --database <database>
However I can't do this for unmigrated applications which are thirdparty.
To be able to sync them I have to run the command without the specifying app
migrate --database <database>
This though ends up with errors from other apps to be routed to different databases complaining that no table is synced.
Is this there anything I can do to resolve the issue.
I appreciate the effort to answer this question.
I guess you can try
python manage.py migrate 'nameoftheapp'
In fact django keep track of the migrations scripts already done in your DB. To check if the list of really applied and non applied migrations run:
python manage.py showmigrations --list
You will see a big X next to the migrations that have already been applied.
If the thirdparty migrations scripts are already applied just delete them from the table django_migrations in you database (created automatically by Django). And apply your migrations again:
migrate <app> --database <database>