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?
'profiles'
from the table 'django_migrations'
.python manage.py makemigrations
and python manage.py migrate
command.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.
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
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.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.
Delete all the migrations in your app and 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";
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"
Run python manage.py makemigrations your-app-name
If you already have an existing database then run python manage.py migrate --fake-initial
and then follow the step below.
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
Run 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With