Generating migration scripts after upgrading django and moving from mysql to postgres - django

We have a django app that is running in production.
It used to run django 1.4.3 against a mysql database.
We had all our migration scripts using django south.
We recently upgraded to django 1.11.6. Along with it we moved the data to a postgres database.
The app runs fine, but the migration scripts that are generated using django migrations by using the django models are not fully consistent with the existing schema.
Almost all of the differences seem to be in the index names that are generated.
What are our options to make the django migrations and the database consistent? How do we go forward with this?
Should we generate a new empty database using the django migrations, and migrate the data from the old to the new empty database?
I know we can edit the models.py and set the index names manually but that is too cumbersome, we need to edit hundreds of models; is there an easy way to do that?
Is there a way I can generate the migration scripts from the existing database, and verify if the models are compatible?

Related

How can I make Django create migration files for tables that aren't Django managed?

Background
I've got a Django app that I want to test. The Django app relies on database tables that were created by a different, non-Django app, but in the same database. Every time I try to run a test I get the following error:
django.db.utils.ProgrammingError: relation "mytable" does not exist
I'm thinking that this error is caused by the fact that the table was created manually and there are no migrations.
Question
Is there a way I can tell Django to build migration files based off of a database table that already exists?
There is a tool called inspectdb in Django for this exact scenario.
This should do the trick:
python manage.py inspectdb > models.py
For reference:
https://docs.djangoproject.com/en/3.2/howto/legacy-databases/

Converting postgresql indexes to Django migrations

We have a Django application that has been in production since Django 1.1. Over the years, we've manually added bells and whistles to the production PostgreSQL db that weren't at the time overtly supported by Django's db automation, especially in the form of custom indexes.
Django's come a long way since 1.1, and now w/ 1.10 I'm pretty sure the migration framework supports all the custom features we've added manually. Is there any automated tool which will compare a database to the models, and generate migrations to bring the models up to date with the db?
This is built into django, you can just run inspectdb
manage.py inspectdb
Introspects the database tables in the database pointed-to by the NAME setting and outputs a Django model module (a models.py file) to standard output. You may choose what tables to inspect by passing their names as arguments.
Use this if you have a legacy database with which you’d like to use Django. The script will inspect the database and create a model for each table within it.

Multiple databases in django with migrations

I have two databases configured, one for all but one tables, table, which I have in the second database, database2. I store data, and retrieve data perfectly without using any router, just the 'using' attribute on the save and the get. I was upgrading our app to django 1.8 a while ago, and it seemed to be working, table in database2 doesn't change that often, but it has been working ok since the upgrade.
But now I need to make a change to table in database2.
So I try manage.py migrate table --database database2. The migration seems to take, but I get an error in the end.
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.
I've been trying to add contentypes and auth as fake migrations in database2, but I still get the error. Will adding a router make it go away? Are there any other ways?
Thanks!

Can I add any column after I created My database?

I created my database using python manage.py syncdb And I tried to add another attribute to my model called created_date My site gives error. And I deleted my db.sqlite3 file Then reorganize my model Then error went. I want to know is this correct
As the comment says migrations are the way to do this. Native migrations were only introduced in Django 1.7 but, since you're using syncdb, I'm guessing you're using an earlier version.
For earlier versions of Django you need a third-party app called South to handle migrations for you. This will then let you change your database after creation fairly painlessly in most cases.

What is the best approach for migrating one Django app to another

I am working on a Django project where I have to use South to migrate one application to another. I have the old internal message application which I have to replace by another completely different. I was wondering if I could pass by orm, but the old application doesn't exist anymore in the INSTALLED_APPS, so no sense. Does using a SQL procedure is the way to do that? I'd like to keep the application DB type independant at the time.
Django applications are namespaced in the database so you ought to be able to temporarily have both applications installed. I would break it down to about three migrations:
A schemamigration to add the new application. If other applications need to have foreign key relations to the new application, add those and just make sure they are all nullable.
A datamigration to walk the model objects in the old application and create the equivalent ones in the new application.
A schemamigration to remove the old application.
schemamigration: python manage.py schemamigration myapp (with nullable foreign keys)
datamigration: Django custom sql is my friend -> https://docs.djangoproject.com/en/dev/topics/db/sql/
have made my custom data migration script keeping the project DB independent
remove the old application schema using 2.
(optional) a backwards rescue script