How to Edit a Database Table Following a Django App Name Change - python

I have just created my first Django app which uses a Postgres database... which was working until I broke it by trying to rename it. I only have placeholder data in the database, and so I am not concerned about database migration, I simply want to get my app working again and with an empty database is just fine. I found this question on here: How to change the name of a Django app? which lays out the steps involved in re-naming a Django app. I have done the first two which are to 1) Rename the folder found in the project root and 2) change any references to the app in its dependencies, i.e. the app's views, the urls.py and settings.py files.
As a newbie to Django and programming, the third step is not clear to me. The instructions were to:
"Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label='' Note: for renaming models, you'll need to change django_content_type.name"
So I have two very specific questions:
1) Where/how do I edit the database table? As in, where do I physically type this command? And/or how do I change the "django_content_type.name"?
2) I have already pushed my project to Heroku and have a working version of my app there. What will I need to do on the Heroku side to handle this change?
Thanks in advance for your patience!

You need to go to the database table manually, or use dbshell.
> ./manage.py dbshell #opens the database command line.
This places you in the command line
$> UPDATE django_content_type SET app_label='<new_label>' WHERE app_label='<old_label>'
$> <ctrl>-D
This is make you all set. You dont need to do anything else on the heroku side apart from this change.

Related

How to use Django with an existing database in MySQL?

I have an application in Django 2.0 and as a database engine I use MySQL. I have a problem because the database was previously created and already has records, my idea is to use this same database for the application I am creating.
Use the command
python manage.py inspectdb > models.py
To create the models.py file which will be cleaned as indicated by the models.py file that was generated.
#This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
After this I proceed to execute:
python manage.py migrate
python manage.py makemigrations
python manage.py migrate
But it generates the following error:
(1050, "Table 'XXXXXXX' already exists")
Obviously it tells me that the table already exists, but how do I not generate this error and continue administering the tables from Django.
You need to run --fake-initial or --fake. See more at Django migrations. Be careful because running inspectdb doesn't solve all your problems. You need to fix the things inside models.py manually and migrate again.
One of the things (and the main reason I do not use Django) is it likes to take control of everything. The fact that it controls the database means that if you don't start strictly in Django, you are doing it wrong.
However there is a work around:
https://docs.djangoproject.com/en/2.0/howto/legacy-databases/

How do you rename apps in Django1.11 LTS? [duplicate]

I have a Django app named app1 with models and migrations files.
I renamed this app to app2 and I fixed all imports, urls etc...
I now have a problem with migrations files and data in tables.
How can I write migrations with the correct way to ensure:
New installation => create the new tables
Update old versions => create new tables, move data, remove old tables
Note 1: there is several tables with many Foreign Keys.
Here is my progress so far and I am not sure if I am on the good way:
I removed all older migrations
I ran python manage.py makemigrations to generate new migrations files
After these 2 steps, I can install my application but I still have problems with old version.
Question: What is the best way to migrate data?
Note 2: I don't use South.
I found a solution that's works
Fix old migrations with new Foreign Keys and new app dependencies.
Force old migrations to create tables with old app name, so for that in migrations.CreateModel.options, add db_table: 'app1_table_name'
In each migration file add replaces = [('app1', 'migration_file_name')]. This will tell to Django that current migration (app2.migration_file_name) will replace the old file, this will prevenent django to execute migrations twice.
Create a migration file to rename tables with migrations.AlterModelTable
This is eloquently answered in this blog post.
The bullet points are:
Rename the folder of the application you want to update.
Update any and import statements to the folder you updated.
Update entries for the django_content_type table to refer to the application's app_label.
Update the table names of any models you haven't explicitly set the table name of. These table names inferred by the application name and need to be updated.
Update entries for the django_migrations table and update the reference for each migration by setting the app field your new app label.
Update any namespaced folder names that are within your /static or /templates folder. For example, you might have
./foo_app/templates/foo_app/index.html and it should be updated to ./bar_app/templates/bar_app/index.html.
Renaming an app is always a tricky issue.
If you do the migration like a simple table renaming migration, at any moment the apps.get_model() for the old app cannot work because the app simply doesn't exist.
I found this answer. I know you are not using south, but I think it might work the same way, just skip the south steps.
Basically, you have to:
Dump the data, before rename, into a json file
Run the script in the answer to rename references in the json file from the app1 to app2
Rename app1 to app2 (all import references, settings.py, etc)
Run the migrations to create the tables for app2
Load the data from json file to the database
Drop the app1 tables
I hope this help.

Django Rebuild all migrations

I am building an app in Django and it uses a live/in-use database.
Basically since the apps development the SQL Database has undergone some structure changes and it is causing issues with Django, Django will try to apply migrations to the database that already exist. For example:
In the Django app I marked the email column as unique which was fine based on the development database. However the main database now always has a table change that marks the email column as unique. Django is fighting this unique key with the one that already exists.
So is it possible to clear all Django migrations and have it make migrations again compared to the more up to date SQL database structure?
If your models are very out of sync with your database, the easiest option is probably to rebuild your models from scratch using inspectdb.
If your models are pretty close to the database already, the first step is to make sure that your models match the database exactly. You can use sqldiff from django-extensions for this.
Once your Django models match your database, follow this answer to re-create migrations based on the existing database schema.

How to change a sqlite table column value type from Django model?

I created a table before I code the Django app and now I merged both the app and the table with following command python manage.py inspectdb > models.py. However after some while I really need to change the value type of one of the column. Is it enough to chage it through the model file or do I need some additional steps?
If you change a field in a Django model, Django itself doesn't know how to update your database accordingly (syncdb only add tables from new models).
You have two options:
manually create your database tables;
use a migration tool like South that detects and generates migration files from changes made to your models;
I recommend the second option as it's programmatic, more error-proof and makes your life easier when you need to go back and forth between database schemas.
There is an easy way to do this. (in Django 2)
After making the necessary changes to the model.py file of your app, run command:
python manage.py makemigrations - This will generate a new file in migration folder of your app.
python manage.py migrate - This will apply those edits on actual databse.
To check if the changes have been applied, run command : .schema <tablename> in your terminal, after entering the sqlite command-line program.

How to update models.py when I create a new table in my database?

I was wondering how to automatically update my models.py in a specific app in django. I created a new table in my database through HeidySQL, when i syncdb, i see the table appears in the directory in the command line, but not in models.py . I also tried migrate and make migrations but still not working.
Any idea? Thanks!
The manage.py migrate and makemigrations commands work in the opposite direction that you are describing here. If you want to generate a starter template of your models based on the current layout of the database, you can try using the inspectdb command.
python manage.py inspectdb > temp_models.py
This will generate a starter model for every table in the database. You should review and update the generated models so they make sense with your app. Take special note of lines ending with the comment # This field type is a guess.
Once you are satisfied with the new models, copy them over to the app's models.py file.
Hope that helps!

Categories

Resources