Hi I am having issues trying to migrate my django database.
I have a database that definitely cant be cleared/removed, which is what most solutions recommend. However I do want the changes I made to my models to be applied to the database.
I have tried all the following command-sets:
python manage.py makemigrations
python manage.py migrate --run-syncdb
python manage.py makemigrations django_project
python manage.py migrate django_project
python manage.py migrate
python manage.py makemigrations && python manage.py migrate
python manage.py makemigrations django_project
python manage.py migrate --fake-initial
python manage.py migrate
I cleared the migrations folder everytime.
Every set resulted in the same response:
08:59 ~/GluoApi $ python manage.py makemigrations && python manage.py migrate
Migrations for 'django_project':
django_project/migrations/0001_initial.py
- Create model Blacklist
- Create model Like
- Create model Post
- Create model PostReaction
- Create model Profile
- Create model Token
- Create model ReportUser
- Create model ReportPost
- Add field reports to profile
- Add field requests to profile
- Add field user to profile
- Create model PostReactionReport
- Create model PostReactionLike
- Add field likes to postreaction
- Add field post to postreaction
- Add field poster to postreaction
- Add field reports to postreaction
- Add field likes to post
- Add field poster to post
- Add field reports to post
- Add field post to like
- Add field user to like
- Create model GiftLink
- Create model AuthToken
- Create model Article
Operations to perform:
Apply all migrations: admin, auth, authtoken, contenttypes, django_project, sessions, sites
Running migrations:
No migrations to apply.
Your models in app(s): 'django_project' have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
Have you cleared the django_migrations table in your database too ?
This table contains probably a tuple ('django_project', '0001_initial') which tell to Django that the initial migration has been already applied, so the migrate command tell you there is not migrations to apply.
But you tell, you can't remove the database, so if you just want to apply migration without make the operations, you can call: makemigrations && migrate --fake, for applying the migrations without really change the database
Related
I made a new app called 'contact' which I added to installed_apps, but I can't create a table in the SQLite database!
Django version = 3.1
python manage.py makemigrations contact
No changes detected in app 'contact'
then for : python manage.py migrate contact
Operations to perform:
Apply all migrations: (none)
Running migrations:
No migrations to apply.
I have tried many solutions proposed by members here but None worked
like checking "init" file existed in migrations folder and it's empty or commands like these :
python manage.py migrate --fake contact zero
Operations to perform:
Unapply all migrations: contact
Running migrations:
No migrations to apply.
python manage.py migrate contact --fake-initial
Operations to perform:
Apply all migrations: (none)
Running migrations:
No migrations to apply.
This worked for me;
python manage.py makemigrations --empty yourappname
python manage.py migrate yourappname
It turns out adding an extra empty migration forces django to recheck the table and in the process, it noticed the new migrations. There's probably some caching taking place somewhere.
Original answer
Try not to use the name of your app.
Eg.
python manage.py makemigrations
python manage.py migrate
If don't work. You can do something more risky. Go to the directory of your project. Take the database to another folder. Make the same above commands.
I am trying to do a migration in django 2.2.4 in python 3.7.
First I try to make do makemigations:
python3 manage.py makemigrations
I get:
Migrations for 'main':
main/migrations/0001_initial.py
- Create model TutorialCategory
- Create model TutorialSeries
- Create model Tutorial
But then I try the second step:
python3 manage.py migrate
I get:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, main, sessions
Running migrations:
No migrations to apply.
Even though a migration should happen.
I tried deleting my migrations folder and then remaking it (with the empty __init__.py file inside) but it still doesn't work.
(Note: I have been following along the tutorial: Linking models with Foreign Keys - Django Web Development with Python p.9 by sentdex)
I faced the same problem in django 2.2, The following worked for me...
delete the migrations folder resided inside the app folder
delete the pycache folder too
restart the server [if your server is on and you are working from another cli]
python manage.py makemigrations <app_name> zero
python manage.py makemigrations <app_name> [explicit app_name is important]
python manage.py migrate
Somehow your migrations are virtually or faked applied in the database, Truncating django_migrations table should work.
Delete all the migrations files:
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete
Truncate table:
truncate django_migrations
makemigrations, migrate.
w/in the app directory I deleted the pycache and migrations folders,
from django.migrations tables I deleted all rows like this for PostgreSQL
DELETE FROM public.django_migrations
WHERE public.django_migrations.app = 'target_app_name';
To delete any app tables already created for the tables to be created from scratch.
Mine didn't migrate cause there was already a record in the django_migrations table with the same name, so I just removed it and then migrate worked.
I am trying to import an existing database into my Django project, so I run python manage.py migrate --fake-initial, but I get this error:
operations to perform:
Apply all migrations: ExcursionsManagerApp, GeneralApp, InvoicesManagerApp, OperationsManagerApp, PaymentsManagerApp, RatesMan
agerApp, ReportsManagerApp, ReservationsManagerApp, UsersManagerApp, admin, auth, authtoken, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... FAKED
Applying auth.0001_initial... FAKED
Applying contenttypes.0002_remove_content_type_name... OK
Applying GeneralApp.0001_initial...Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/IntellibookWebProject/IntellibookWebVenv/lib/python3.6/site-packages/django/db/back
ends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: relation "GeneralApp_airport" already exists
Of course all the tables already exist in the database, that is the reason why I use --fake-initial, that is supposed to fake the creation of database objects.
Why is migrate attempting to create the table GeneralApp__airport instead of faking it?
Just by running the following command would solve the problem:
python manage.py migrate --fake
--fake-initial can't deal with any situation where some of the tables listed in the initial migration exist and some do not. So, if tables exist for some, but not all, of the CreateModel()s in the operations list in your 0001_initial.py, --fake-initial does not apply and it tries to create tables for ALL of the models.
Does that make sense? Not to me...
Steps you can follow to make migrations from an existing database. Firstly empty the django migration table from the database.
delete from django_migrations
Remove migrations from your migrations folder for the app
rm -rf <app>/migrations/
Reset the migration for builtin apps(like admin)
python manage.py migrate --fake
Create initial migration for each and every app
python manage.py makemigrations <app>
The final step is to create fake initial migrations
python manage.py migrate --fake-initial
I recently added a model to my app (UserProfile) and when I pushed the changes to Heroku, I think I accidentally ran heroku run python manage.py makemigrations. Now when I try to run heroku run python manage.py migrate I get the error below
(leaguemaster) benjamins-mbp-2:leaguemaster Ben$ heroku run python manage.py migrate
Running `python manage.py migrate` attached to terminal... up, run.1357
Operations to perform:
Synchronize unmigrated apps: allauth
Apply all migrations: auth, admin, socialaccount, sites, accounts, account, contenttypes, sessions, leagueapp
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
How do I fix this? Please help!
You need to first create the migrations locally, add them to your repository, commit the files with the new migrations and then push to heroku.
The sequence is something like this:
1. (add/modify some someapp/models.py)
2. python manage.py makemigrations someapp
3. python manage.py migrate
4. git add someapp/migrations/*.py (to add the new migration file)
5. git commit -m "added migration for app someapp"
6. git push heroku
7. heroku run python manage.py migrate
1. Make migrations locally
$ python manage.py makemigrations && python manage.py migrate
2. Commit changes and push it on the server
$ git add --all
$ git commit -m "Fixed migrate error"
$ git push heroku master
3. Now run migrate on the server
$ heroku run python manage.py migrate
You also need to be sure that you haven't ignored these migration paths in your .gitingnore file
It sounds like you ran makemigrations after you made changes to your model but before you had an initial migration file. Try to revert your app to the state it was before you added the new model and run makemigrations again to create the initial migration. Then add your updates back in and run makemigrations once more. This will create a second migration from your initial data structure to the new updated one. Then try your deployment.
https://docs.djangoproject.com/en/1.7/topics/migrations/#adding-migrations-to-apps
Answered for my case:
your_field=models.CharField(max_length=9,default=False)
Convert to
your_field=models.CharField(max_length=9,default='False')
My case:
In models.py for the field I wanted to set the default value to False. First default = False my contract without ''. But after running python manage.py migrate, I got the above error. The problem was solved after placing False inside ''.
Sometimes it is necessary for the default value in our field model to be a False string type.
If
default = False
If you write in the model, you will encounter this error.
In fact, depending on the type of field we have, we can not always set the default value of a field in the model to True or False. Must be converted to string for CharField type.
I have an existing django app and would like to add a field to a model.
But because the website is already in production, just deleting the database is not an option any more.
These are the steps I took:
pip install south
added 'south' to INSTALLED_APPS
python manage.py syncdb
python manage.py convert_to_south [myapp]
So now I have the initial migration and south will recognize the changes. Then I added the field to my model and ran:
python manage.py schemamigration [myapp] --auto
python manage.py migrate [myapp]
Now I have the following migrations:
0001_initial.py
0002_auto__add_field_myapp_fieldname.py
Which commands should I run on my production server now to migrate? Also should I install south first and then pull the code changes and migrations?
first is to fake the initial migration:
python manage.py migrate [yourapp] --fake 0001
then you can apply the migration to the db
python manage.py migrate [yourapp]
I'm assuming you ran convert_to_south on development, in which case production still wouldn't be aware of the migrations yet. convert_to_south automatically fakes the initial migration for you! If you were to just run migrate on production without faking, it should error.