I'm developing a Django 1.8 application locally and having reached a certain point a few days ago, I uploaded the app to a staging server, ran migrations, imported the sql dump, etc. and all was fine.
I've since resumed local development which included the creation of a new model, and changing some columns on an existing model. I ran the migrations locally with success, but after rsync-ing my files to the staging server, I get a 'relation already exists' error when running manage.py migrate. And when I visit the admin page for the new model, I get a 'column does not exist' error.
It seems as though the migrations for this model were partially successful but I cannot migrate the entirety of the model schema. I've tried commenting out parts of the migration files, but was not successful. Would it be possible to create the missing columns via psql? Or is there some way of determining what is missing and then manually write a migration to create the missing database structure?
I'm using Django 1.8.6, Python 3.4.3, and PostgreSQL 9.3.6. Any advice on this would be great. Thanks.
Try running migrate --fake-initial since you're getting the "relation already exists" error. Failing that, I would manually back up each one of my migration folders, remove them from the server, then re-generate migration files for each app and run them all again from scratch (i.e., the initial makemigrations).
Related
I'm a new django developer and I recently started a project, and am trying to get the database up and running.
For background: we are using a mysql database for storage, and the settings are configured for it. Using an Unbuntu DO server. I have also been using the virtual environment to run the commands below.
When we initially created the project we ran the makemigrations command, which populated the database with django tables, but not the Models, as we had not made any Models yet. I've now uploaded the Model code, however I could not make any new migrations, I got the error RuntimeError("Settings already configured").
According to this tutorial which I was following (https://www.geekinsta.com/how-to-customize-django-user-model/) some errors are normal and required me to delete and recreate the mysql database (which I did, it has the same name), and clear past migrations (the app/migrations/pycache is now empty). However I'm still experiencing the same error ("Settings already configured").
How can I fix this? I need to successfully run the makemigrations and migrate commands to populate the database. The new database and pycache are empty after I attempted the commands again. Thanks for any and all help.
I am unable to run the inital migrations in my django project on my production server.
I started working on my Django app on my computer with a sqlite db. I was able to run the initial migrations (auth, admin, etc.) and they created tables in my sqlite db with no problem. I was also able to get tables created for the models in my app.
Today I moved the same django project to my webfaction server via git and I am unable to get my MySQL db populated with tables at all. As soon as I created the db, I ran migrate but it said there were no migrations to apply. I tried deleting the migrations folder in my app, but that didn't help at all, probably because there is no django_migrations table to compare any migrations against.
It seems as if the initial migrations are stored where django is installed. This is not part of my git repo, so anything there that might indicate whether the migrations have been made wouldn't be pulled over.
I tried running python manage.py migrate admin
And the same thing with auth, but it did't work
I'm not really sure what the proper way is to go about getting these initial migrations to run again so that I have the proper auth and admin tables in my db. Any help would be appreciated.
This is Django 2.1.7 and Python 3.5
Thanks, Yongjin and Nagesh. By asking me to post my DB settings, you made me realize that I hadn't changed the name of the DB from the project I copied the settings from. Much appreciated!
I started creating my website on my own PC using the Django-cms install script then I tried to add a lot of applications such as Django-Helpdesk and others and everything was smooth until I tried to add pieces of djangoSHOP demo project and dependencies to email_auth.
Then I started to have report that the migration history was inconsistent and migrations applied out of order.
Before adding djangoShop to the installed apps, I just add the following commands to setup the DB:
manage.py migrate --run-syncdb --noinput
manage.py migrate --noinput
and everything was ok.
Now, as I'm just starting, I don't have changes to the schema to apply but most of the django apps I try to install have migrations folders with several migrations.
I don't want to update an existing schema, I want to create a new one.
I'm working from a blank database so do I really need to apply all those migrations ? To me it seems that the default behaviour of running migrations is not compatible with starting from a blank db,
I don't understand what to do just initialise the database to just have the required tables created.
Did anyone got the same kind of problems?
How did you fixed it?
Edit:
django-cms>=3.5,<3.6
Django 1.11.19
Error message:
Django InconsistentMigrationHistory: Migration Helpdesk.000xxxxx.py is applied before its dependency email_auth.0001_initial.py on database 'default'
So, I committed and pushed all my code, and then deployed my web application successfully. Then, I added a new model to my 'home' app, which (for a reason I now understand, but doesn't matter here), created an IntegrityError (django.db.utils.IntegrityError: insert or update on table "foo" violates foreign key constraint "bar"). I ran python manage.py makemigrations, python manage.py migrate, which causes the the IntegrityError.
However, even if I remove all of my new model code(so that git status comes up with nothing), the IntegrityError still happens. If I connect to my db via a different python instance and download select * from django_migrations;, the latest db migration: 0020 there is eight migrations away from my latest local home/migrations migration file: 0028.
--> My question is: is it safe for me to delete my local 0021-0028 migration files? Will this fix my problem?
If you haven't applied your migrations to db, it is safe to delete them and recreate them.
Possible reasons of why you run into this error are:
You deleted your model code but, when you run migrate it reads your migration files (which has information about your deleted model) and tries to apply migration operations. If you didn't run makemigrations command after you've deleted your model, migration system won't be able to detect your changes and will think that your model is still there.
Even if you've run makemigrations after you've deleted your model there'll be dependency issues in your migrations files, because the new migration files will depend on old ones (with which you had problems)
That's why we can say that it is safe to delete them, if they haven't applied, but at the same time you should be careful with your migration dependencies.
This documentation information maybe useful.
OK, so I crossed my fingers, backed my local 0021-0028 migration files, and then deleted them. It worked. I think they key is that the migration files were not yet in the database yet, but not 100% sure. +1 if anyone can answer further for clarification.
I'm in the process of preparing a Django application for its initial production release, and I have deployed development instances of it in a few different environments. One thing that I can't quite get happening as smoothly as I'd like is the initial database migration. Given a fresh installation of Django, a deployment of my application from version control, and a clean database, manage.py migrate will handle the initial creation of all tables (both Django's and my models'). That's great, but it doesn't actually create the initial migration files for my apps. This leads to a problem down the road when I need to deploy code changes that require a new database migration, because there's no basis for Django to compute the deltas.
I've tried running manage.py makemigrations as the first step in the deployment, in the hopes that it would create the migration files, but it reports that there are no changes to migrate. The only way I've found to get the baseline state that I need is to run manage.py makemigrations [appname] for each of my apps. Shouldn't makemigrations, called without a specific app name, pick up all the installed apps and create their migrations? Where am I going wrong?
You're going wrong at the very end -- yes, you do need to call manage.py makemigrations <appname> for each of your apps once. It's not automatically done for all apps.
Presumably that is because Django has no way of knowing if that is what you want to do (especially if some apps were downloaded from PyPI, etc). And a single command per app can't really be an extreme amount of work, right?