Your models have changes that are not yet reflected in a migration - django

In django1.9, tables have been in database already, i create init migration files
python manage.py makemigrations my_app
then, i run migrate:
python manage.py migrate my_app
It shows:
psycopg2.ProgrammingError: relation "p_record_segment" already exists
I want to fake it, first, i clean the django_migrations, then excute:
python manage.py migrate my_app --fake 0001_initial
It shows:
Running migrations:
Rendering model states... DONE
Applying my_app.0001_initial... FAKED
I think this will be fine, but when i run migrate again:
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.
I saw the history in django_migrations, and no changes in fact, but why django show me this.

Try using --fake-initial instead of just --fake

Related

python manage.py migrate --fake <appname> vs python manage.py migrate --fake <appname> zero

When I run python manage.py migrate --fake photos zero :
Operations to perform:
Unapply all migrations: photos
Running migrations: Rendering model states... DONE
Unapplying
photos.0001_initial... FAKED
After running above command, I ran python manage.py migrate at that time this error occurred.
Traceback:Traceback (most recent call last):
File
"C:\Users...\lib\site-packages\django\db\backends\sqlite3\base.py",
line 411, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: table "photos_userphoto" already
exists
When I run python manage.py migrate --fake photos:
Operations to perform: Apply all migrations: photos Running
migrations: Applying photos.0001_initial... FAKED Applying
photos.0002_auto_20210303_0120... FAKED
After running above command, I ran python manage.py migrate and this work perfectly.
Running migrations:
Applying photos.0003_auto_20210303_0123... OK
So my question is that what difference between python manage.py migrate --fake <appname> vs python manage.py migrate --fake <appname> zero and Why first command throws me an error?
Thank you..
You appear to have a misunderstanding about the --fake flag. According to the documentation the --fake flag:
Marks the migrations up to the target one (following the rules above)
as applied, but without actually running the SQL to change your
database schema.
It also further states that it is meant for advanced users if they are making changes manually. Basically Django makes a table django_migrations to manage your migrations. It adds an entry to it to mark if a migration has been applied. What --fake does is simply add / remove the entries to / from this table according to the migration you specify. Also the zero means to undo all migrations.
python manage.py migrate --fake photos zero
The above command means you basically say to Django:
I have or will manually remove(d) all the tables, etc. from the database
for the app photos
python manage.py migrate --fake <appname>
In the above command you don't specify the migration (in previous you specified zero), this means to assume that you mean till the most recent migration. Basically with --fake this command means:
I have or will manually make all the tables, etc. in the database for
the migrations till the most recent one.
From my understanding you only made some changes and wanted to update your database, for this you could have simply written:
python manage.py migrate

How to redo a migration on django 1.8 after using --fake

Something went wrong on my migrations, I added a new datetimefield to a model then I used makemigrations and migrate.
python manage.py makemigrations
python manage.py migrate
But after this the migrate got an "table already exists error". I supposed I could fake the migrations and start over, so I did
python manage.py makemigrations --fake core
Operations to perform:
Apply all migrations: core
Running migrations:
Rendering model states... DONE
Applying core.0001_initial... FAKED
Applying core.0002_auto_20150525_1331... FAKED
Applying core.0003_auto_20150525_1348... FAKED
Applying core.0004_processo_data_atualizacao... FAKED
but the new migrate that I've just created was faked too (of course!).
How is the proper way to redo a migration (in this case the core.0004) after doing this?
You should first set your current state to 0003 with --fake (assuming 0003 is the last migration you really have applied):
python manage.py migrate --fake core 0003
And then proceed as usual:
python manage.py migrate core
Relevant documentation: https://docs.djangoproject.com/en/dev/ref/django-admin/#migrate

Cannot get Django 1.7 Migrations to detect proper changes to my DB.

I have a production web project running with a decent amount of data in the MySQL db. I am trying to update the database with some changes to an app called "enterlink." I've made new elements in the existing models and created new models altogether. Before this migration, I have never touched the schema of the db since originally running syncdb to create it. When I run: "python manage.py makemigrations enterlink" the below output appears(pic). My question is, why is this happening? The DB already includes all the models that it lists in the picture so why is it registering those lists of models? When I go to finish the migration by doing "python manage.py migrate" or "python manage.py migrate --fake enterlink" (pic again), I get an output shown but my database schema remains identical to the old db and any new code generates errors. Can anyone tell me what is likely the problem? I would be really appreciative of any advice. It's been very frustrating since I'm not sure what I'm missing.
What you have done is that you have ran the command python manage.py syncdb before running python manage.py makemigrations myapp and python manage.py migrate myapp. That is why syncdb created the database schema and the migration was faked because schema already exists. I will suggest to use python manage.py makemigrations myapp and python manage.py migrate myapp and not to use syncdb as its deprecated in Django 1.7.
If you change anything in your model, just run makemigrations and migrate command. Syncdb isn't necessary.
This question and relevant answers are intriguing me. Thus I want to share my experience on maintaining live database and migrations.
Tested in django1.5.5
Initializing the database:
./manage.py syncdb --noinput
./manage.py migrate
./manage.py syncdb
Now I have created the database.
Doing a migration for an app:
./manage.py schemamigration myapp --initial
./manage.py migrate myapp --fake
Now do necessary changes in your model
./manage.py schemamigration myapp --auto
./manage.py migrate myapp
Im newbie for schemamigration too, but i will explain how it works for me:
First you create app and then
./manage.py sycndb, so tables are created then you can
./manage.py makemigrations myapp --initial
so now initial migrations are created and you should apply them
./manage.py migrate myapp
now you can change your models : add,change fields, anything you want and then
./manage.py makemigrations myapp --auto
this will create migrations for changes and now you need to apply them
enter code here./manage.py migrate myapp
so this actually will create new tables in db

Migrations failing on Heroku

I have a Django-CMS project running on a Cedar stack using Django 1.7.1 and Python 3 at Heroku.
I can create a migration just fine using: heroku run python manage.py makemigrations which returns:
Migrations for 'zinnia':
0002_entry_content_placeholder.py:
- Add field content_placeholder to entry
However, running the migration via: heroku run python manage.py migrate returns:
Operations to perform:
Synchronize unmigrated apps: mptt, tagging, djangocms_admin_style, localflavor, django_comments, robots, ckeditor, sekizai, compressor, cmsplugin_plaintext, storages
Apply all migrations: admin, cms, zinnia, sessions, contenttypes, sites, auth, djangocms_link, djangocms_file, djangocms_picture, djangocms_text_ckeditor, menus
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.
Re-running makemigrations and migrate simply returns the same error statement. What am I doing wrong?
im not so sure on that one, but after you ran the Migration, the file created:
0002_entry_content_placeholder.py
is as you notice, the second migration file.
If its an option for you, you could try to delete/rename the migration folder/files and run makemigrations again.
I got kinda the same error some time ago, and if i remember right, that did the trick.
Sorry for bad english

How to do a syncdb with django, when migrations are also involved

when I do a syncdb I get the following error everytime:
Not synced (use migrations):
- deals
- analytics
(use ./manage.py migrate to migrate these)
And when I run sudo python manage.py migrate. I get the following
Running migrations for deals:
- Nothing to migrate.
- Loading initial data for deals.
No fixtures found.
Running migrations for analytics:
- Nothing to migrate.
- Loading initial data for analytics.
No fixtures found.
I highly appreciate your help
From the output, it seems like the database is already synchronized with the migrations. There are no problematic errors. (Although you shouldn't really be root to run the migrations.)
If you're looking into creating more migrations, use the south documentation, which usually is just running the following after you modify the models:
python manage.py schemamigration --auto <APP>
And then use python manage.py migrate to apply the changes.
It looks like migrations have been already passed. Check south_migationhistory table in db.
If you want to sync new db for apps which has migrations just disable south in settings.py.
Have you ran a schemamigration initial yet?
./manage.py schemamigration deals --initial
./manage.py migrate deals
if you get the error, db already excists do this:
./manage.py schemamigration deals --initial
./manage.py migrate deals --fake