I use Django1.7 with Mezzanine. I create simple profile (according to Mezzanine documentation) stored in separate app “profiles”:
class RoadmapProfile(models.Model): user = models.OneToOneField("auth.User") fullname = models.CharField(max_length=100, verbose_name="Full name")
Creation of migrations returns:
Migrations for 'profiles': 0001_initial.py: - Create model RoadmapProfile
When I run “migrate profiles”:
Operations to perform: Apply all migrations: profiles Running migrations: No migrations to apply.
The issue is, when I try to open any page related to mezzanine.accounts (for example update account), it crashes with:
OperationalError at /accounts/update/ no such column: profiles_roadmapprofile.fullname
What I have done wrong?
Sounds like your initial migration was faked because the table already existed (probably with an outdated schema):
“This will make a new initial migration for your app. Now, when you
run migrate, Django will detect that you have an initial migration and
that the tables it wants to create already exist, and will mark the
migration as already applied.”
Otherwise you would get an no-such-table error 🙂
 did you clean up the applied-migrations table? That’s also a common cause for non-applied migrations.
- In MySQL Database delete row
'profiles'from the table
- Delete all migration files in migrations folder.
- Try again
python manage.py makemigrationsand
python manage.py migratecommand.
I am a Django newbie and I was going through the same problem. These answers didn’t work for me. I wanted to share how did I fix the problem, probably it would save someone lots of time.
I make changes to a model and I want to apply these changes to the DB.
What I did:
Run on shell:
python manage.py makemigrations app-name python manage.py migrate app-name
No changes are made in the DB
But when I check the db schema, it remains to be the old one
- When I run python
manage.py migrate app-name, Django checks in django_migrations table in the db to see which migrations have been already applied and will skip those migrations.
What I tried:
Delete the record with app=”my-app-name” from that table (
delete from django_migrations where app = "app-name"). Clear my migration folder and run
python manage.py makemigration my-app-name, then
python manage.py migrate my-app-name. This was suggested by the most voted answer. But that doesn’t work either.
Because there was an existing table, and what I am creating was a “initial migration”, so Django decides that the initial migration has already been applied (Because it sees that the table already exists). The problem is that the existing table has a different schema.
Drop the existing table (with the old schema), make initial migrations, and applied again. This will work (it worked for me) since we have an “initial migration” and there was no table with the same name in our db. (Tip: I used
python manage.py migrate my-app-name zero to quickly drop the tables in the db)
Problem? You might want to keep the data in the existing table. You don’t want to drop them and lose all of the data.
Create an initial migration with the same schema as the existing table, with these steps:
Modify your models.py to match with the current table in your database
Delete all files in “migrations”
python manage.py makemigrations your-app-name
Delete in django_migrations all the fields with django_migrations.app = your-app-name
How to do this depends on which DB you are using
Example for MySQL:
delete from django_migrations where app = "your-app-name";
Modify your models.py to match the new schema (e.i. the schema that you need now)
Make new migration by running
python manage.py makemigrations your-app-name
python manage.py migrate your-app-name
This works for me. And I managed to keep the existing data.
The reason I went through all of those troubles was that I deleted the files in some-app/migrations/ (the migrations files). And hence, those migration files and my database aren’t consistent with one another. So I would try not modifying those migration files unless I really know what I am doing.
1- run python
manage.py makemigrations <appname>
2- run python
manage.py sqlmigrate <appname> <migrationname> – you will find migrationname in migration folder under appname (without ‘.py’ extension of course)
3- copy all text of result # all sql commands that generated
4- go to your db ide and paste as new query and run it
now all changes are applied on your db
python manage.py migrate --fake APPNAME zero
This will make your migration to fake. Now you can run the migrate script
python manage.py migrate APPNAME
Tables will be created and you solved your problem.. Cheers!!!
In my case I wrote like this:
python manage.py makemigrations –empty
python manage.py migrate yourappname
Django keeps track of all the applied migrations in django_migrations table. So just delete all the rows in the django_migrations table that are related to you app like:
DELETE FROM django_migrations WHERE app='your-app-name
and then do:
python manage.py makemigrations
python manage.py migrate
The problem here are fake migrations somewhere. Basically in your database the table created from your model doesn’t exist, tho somewhere in time that table existed before, due an old update o whatever it may be. The problem is that django already made those migrations so the tables MUST exist for hence overlooking migrations but getting error “table_doesnt_exist” in Admin.
1.- Make sure to save any data from that model.
2.- Access your database and run this query.
SELECT * FROM django_migrations;
3.- Get the id from the list generated from the query. These are migrations that Django has migrated so far, hence TABLES MUST EXIST. In your case I’d look for a row named roadmapprofile, due this is the name of your model.
4.- Now lets delete this row from this table using the ids,
DELETE FROM django_migrations where django_migrations.id in (value_id1, value_id2 ... value_idN);
Replace value_id1 and value_id2 with respective ids. It could be only one or many so don’t worry if you don’t see more than 1 id, what this means is that only one model exists under the current app.
5.- Migrate app to zero
manage.py migrate <app_name> zero
6.- Delete all migrations files within the app migrations folder
7.- Create Migrations
8.- Once you delete these registries and run manage.py migrate; Django will be forced to run migrations for these models due “MIGRATIONS WON’T EXIST” for these models.
That’s it. You shouldn’t have any problems following these instructions. By the way, you shouldn’t loose any data from other models due you’re only migrating and updating tables related to these specific models.
My issue was that there was no
__init__.py file in the same folder as the migrations. On adding the
__init__.py to the folder which contained them,
manage.py migrate found and ran them.
@phanhuy152 has the best answer. Just to add my two cents:
His solution is:
- Delete migration history in DB
- Edit your model to be consistent with DB before your change
makemigrationsto restore the initial state of the migration files
- Then, change the model as you like
migrateto apply updates to table.
But in my case, I have several models in the
models.py file and at the last step, Django complains about
Table xxx already exists, because the initial migrations files intends to create the xxx table again, when we just don’t (and don’t want to)drop other tables.
In this case, in order to preserve the data, we must tell Django to leave them alone in
migrate. We just do: (assume that class A is the one we change, and class B, C remain same):
from django.db import models class A(models.Models): ... class B(models.Models): class Meta: managed = False # tell Django to leave this class alone ... class C(models.Models): class Meta: managed = False # tell Django to leave this class alone
Add these lines after we construct the initial migrations.
So, the process now is:
managed = Falseto other classes
Metachanges. You will see something like:
Migrations for 'backEnd': backEnd/migrations/0002_auto_20180412_1654.py - Change Meta options on toid - Change Meta options on tprocessasinc - Change Meta options on tservers - Change Meta options on tsnmpserver
migrateto apply them in DB
- Change your model now: add a field, change the type, etc.
- Delete the
Metaclass to let Django manage other class again.
Now you have all the structure and data of your models, without losing the part formerly stored in DB.
I had this same problem. Make sure the app’s migrations folder is created (YOURAPPNAME/ migrations). Delete the folder and enter the commands:
python manage.py migrate --fake python manage.py makemigrations <app_name> python manage.py migrate --fake-initial
I inserted this lines in each class in models.py:
class Meta: app_label = '<app_name>'
This solved my problem.
For me, none of the offered solutions worked. It turns out that I was using different settings for migration (
manage.py) and running (
wsgi.py). Settings defined in
manage.py used a local database however a production database was used in
wsgi.py settings. Thus a production database was never migrated.
for migration proved to be better as you have to specify the settings used as here.
- Make sure that when migrating you always use the same database as
Maybe your model not linked when migration process is ongoing.
Try to import it in file
from models import your_file