So i'm following the Tango with Django guide, specifically part 7.3.2. of the tutorial. After I ran the command python3 manage.py makemigrations rango, it won't let me re-populate the database using the populate script. Instead I got the error: django.db.utils.OperationalError: no such column: rango_category.slug.
Then, I tried to revert things back to normal by deleting all the slug code (so before 7.3 of the guide). I made a migration after doing so, but then because the prior migration which added the slug category was never deleted, I can't use the migrate command to revert my changes because it throws an error when it tries to migrate using the old Added category slug migration. Even when I specifically point to the newest migration, it still gives me an error for the old one.
Then, in an effort to fix things, I used a bunch of different migrate and makemigrations commands, including --empty, --fake, squashmigrations etc and now my migrations look unrecognizable. Is there a way to just delete all these existing migrations and just start from a completely clean slate?
This is what migrate --list looks like now, for reference:
admin
[ ] 0001_initial
auth
[ ] 0001_initial
rango
[ ] 0001_squashed_0003_category_slug (3 squashed migrations)
[ ] 0001_squashed_0005_auto_20150328_0917 (5 squashed migrations)
contenttypes
[ ] 0001_initial
sessions
[ ] 0001_initial
For a clean start.
Delete the migrations folder inside rango
delete database which created.Then restart your migration process
You don't need to delete the migrations folder, only the migrations files, but leave __init__.py there.
Related
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.
Actually, what I do is:
Delete the migration file.
Delete the row from the table of django_migrations on the database.
Delete the changes applied by the migration that I want to delete or unapplied.
I want to know if there's another way to do this.
You can revert back by migrating to the previous migration. See the migrations folder of your app and then see all the migrations
Say for an example, if your migrations are something like below ordered number wise and latest migration 0012_latest_migration is applied currently.
0010_previous_migration
0011_next_migration
0012_latest_migration
And You want to go back to 0010_previous_migration
./manage.py migrate my_app 0010_previous_migration
and then you can delete all migrations after that like here delete both 0011_next_migration and 0012_latest_migration as you already applied 0010_previous_migration.
If you're using Django 1.8+, you can show the names of all the migrations with
./manage.py showmigrations my_app
To reverse all migrations for an app to initial or start, you can run:
./manage.py migrate my_app zero
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 am trying to upgrade from Django 1.6.7 to Django 1.7.1, so I have been trying to migrate my app.
I have followed the django docs here.
I deleted the south from my installed apps.
In the migration directory, I delete the numbered migration files and the .pyc files but I kept the directory & __ init__.py file.
I then run :
python manage.py makemigrations your_app_name
I receive the following confirmation message:
Migrations for 'your_app_name':
0001_initial.py:
- Create model UserProfile
Next I run:
python manage.py migrate your_app_name
I received the following error:
CommandError: App 'your_app_name' does not have migrations (you cannot selectively sync unmigrated apps)
As per the docs, I also ran:
python manage.py migrate --fake your_app_name
I received the same error message:
CommandError: App 'your_app_name' does not have migrations (you cannot selectively sync unmigrated apps)
Can anyone shed some light on why this will not work for me?
I noticed that only those apps that actually contain a migrations folder that contain a file __init__.py are recognized by migrations. IE having only models.py in your app is not enough.
If you have a single app, running migrate without specifying the app or migration may work.
If so, the first thing to check is that your app name matched that specified in your settings.py under INSTALLED_APPS.
As pointed out in the comments, app names can be in the form [parent_app].[app_name]. In this case, migrate needs [app_name] only.
Your app must contain a models.py file (even emtpy).
Source: https://groups.google.com/forum/#!msg/django-users/bsTZEmxgDJM/wH0p3xinBWIJ
Just to mention another possible reason:
In my Django app i added the correct migrations and installed the app with pip and got the same error.
What i was missing is a correct MANIFEST.in file
Also the parameter include_package_data in setup() from the setup.py file was not set to True.
First, I create my database.
create database mydb;
I add "south" to installed Apps. Then, I go to this tutorial: http://south.aeracode.org/docs/tutorial/part1.html
The tutorial tells me to do this:
$ py manage.py schemamigration wall --initial
>>> Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate wall
Great, now I migrate.
$ py manage.py migrate wall
But it gives me this error...
django.db.utils.DatabaseError: (1146, "Table 'fable.south_migrationhistory' doesn't exist")
So I use Google (which never works. hence my 870 questions asked on Stackoverflow), and I get this page: http://groups.google.com/group/south-users/browse_thread/thread/d4c83f821dd2ca1c
Alright, so I follow that instructions
>> Drop database mydb;
>> Create database mydb;
$ rm -rf ./wall/migrations
$ py manage.py syncdb
But when I run syncdb, Django creates a bunch of tables. Yes, it creates the south_migrationhistory table, but it also creates my app's tables.
Synced:
> django.contrib.admin
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> south
> fable.notification
> pagination
> timezones
> fable.wall
> mediasync
> staticfiles
> debug_toolbar
Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)
Cool....now it tells me to migrate these. So, I do this:
$ py manage.py migrate wall
The app 'wall' does not appear to use migrations.
Alright, so fine. I'll add wall to initial migrations.
$ py manage.py schemamigration wall --initial
Then I migrate:
$ py manage.py migrate wall
You know what? It gives me this BS:
_mysql_exceptions.OperationalError: (1050, "Table 'wall_content' already exists")
Sorry, this is really pissing me off. Can someone help ? thanks.
How do I get South to work and sync correctly with everything? The only thing I can think of is remove my app from INSTALLED_APPS, then run syncdb, then add it back on.
That is SO SILLY.
South allows you to create migrations when you first start out with a new app and the tables haven't been added to the database yet, as well as creating migrations for legacy apps that already have tables in the database. The key is to know when to do what.
Your first mistake was when you deleted your migrations, as soon as you did that, and then ran syncdb, Django didn't know that you wanted south to manage that app anymore, so it created the tables for you. When you created your initial migrations and then ran migrate, south was trying to create tables that django already created, and thus your error.
At this point you have two options.
Delete the tables for the wall app from your database and then run $ py manage.py migrate wall This will run the migration and create your tables.
Fake out the initial migration run
$ py manage.py migrate wall 0001 --fake This will tell south that you already have the tables on the database so just fake it, which will add a row to the south_migrationhistory table, so that the next time you run a migrate it will know that the first migration has already been run.
Setting up a brand new project and no database
create your database
add south to installed apps
run syncdb, this will add the django and south tables to the database
add your apps
for each app run python manage.py schemamigration app_name --initial this will create the initial migration files for your app
then run south migrate python manage.py migrate app_name this will add the tables to the database.
Setting up a legacy project and database
add south to installed apps
run syncdb, this will add the south tables to the database
for each of your apps run python manage.py schemamigration app_name --initial This will create your initial migrations
for each of your apps run python manage.py migrate app_name 0001 --fake , this will fake out south, it won't do anything to the database for those models, it will just add records to the south_migrationhistory table so that the next time you want to create a migration, you are all set.
Setting up a legacy project and no database
create database
add south to installed apps
for each of your apps run python manage.py schemamigration app_name --initial This will create your initial migrations
run syncdb, this will add any apps that don't have migrations to the database.
then run south migrate python manage.py migrate this will run all migrations for your apps.
Now that you are setup with south, you can start using south to manage model changes to those apps. The most common command to run is python manage.py schemamigration app_name migration_name --auto that will look at the last migration you ran and it will find the changes and build out a migration file for you. Then you just need to run python manage.py migrate and it alter your database for you.
This is how I get things working.
pip install South
# add 'south', to INSTALL_APPS, then
python manage.py syncdb
# For existing project + database
python manage.py convert_to_south app_name
# Thereafter, call them per model changes
python manage.py schemamigration app_name --auto
python manage.py migrate app_name
References:
http://garmoncheg.blogspot.com/2011/08/django-how-and-why-to-use-migrations.html
http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/
The tutorial you're using states:
(If this fails complaining that
south_migrationhistory does not exist,
you forgot to run syncdb after you
installed
South.)
Assuming that your post accurately details the steps you've taken, following that link seems to show that you missed a step before setting up your new app. As you are following a tutorial for setting up migrations on a new application, the order is:
Add south to INSTALLED_APPS.
Run syncdb.
Then follow the tutorial.
I.e., you should've already run syncdb before you added in the models for your new app. Your solution of removing your app from INSTALLED_APPS should work, but it's worth noting that it's really only a "silly" work-around, as you missed a step earlier on. Had syncdb been run before you created the models for that app, you wouldn't have to use the work-around.
Just for future ref. If South is giving you any problems:
Remove the migrations directories from your app directories
Delete South_migrations from your database
Run manage.py syncdb
Go back to using South (e.g. './manage.py convert_to_south something, ./manage.py migrate ...')
This seems obvious, but I'd highly recommend reading the docs.
Even after reading the answers to this question I struggled to understand how to use South effectively.
That all changed of course the day I read the docs and you should too, South is simpler to use than you might think.
http://south.aeracode.org/docs/about.html
http://south.aeracode.org/docs/tutorial/index.html
http://south.aeracode.org/docs/convertinganapp.html#converting-an-app
I also found this useful:
http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/
And make sure you read Jeff Atwood's Coding Horror articles on database version control.
How do I get South to work and sync
correctly with everything? The only
thing I can think of is remove my app
from INSTALLED_APPS, then run syncdb,
then add it back on.
I have used that fix with South troubles in the past. Not a pretty solution but very effective ;)
But the main problem is that your order isn't correct. You should have run syncdb before the tutorial. Than it works properly.