Django migrations for second model - django

Django doesn't respond to making the migrations to the model in the new app.
I deleted the app then made a new one, but still doesn't respond. I deleted the old migration and makemigrations, Django only respond to the first model which I have done before this problem
(venv) F:\My site>python manage.py makemigrations
No changes detected
(venv) F:\My site>python manage.py makemigrations blog
No installed app with label 'blog'.

What you have to do is set your new app in the INSTALLED_APPS of Django settings.py.
INSTALLED_APPS = [
...,
'blog'
]

Related

How to run a migration for a specific app in Django

What I need
I added an new app in my project and a separate database for it. I have created the Models for that app and now I need to migrate this models in this separate database
app name - authors_app
database name - authors
My code
python3 manage.py makemigrations authors_app
It creates the 0001_initial.py file which contains only models from my authors_app. So this step is OK
python3 manage.py migrate authors_app 0001_initial --database=authors
And this command runs not only my migrations from authors_app, but also migrations from my other app
Problem
I need to migrate only migrations from authors_app. But the migrate command runs the migrations from all apps. I have 58 migrations in my other app. And this command runs them all into the new database ...
Question
How can I run migrate for only authors_app
Update
Inside my authors_app Models I use one model from another app
from tibrains_app.models import Language
class AuthorLanguage(models.Model):
author = models.OneToOneField(Author, on_delete=models.CASCADE)
native_list = models.ManyToManyField(Language, related_name='author_native_languages')
all_list = models.ManyToManyField(Language, related_name='author_all_languages')
writing_list = models.ManyToManyField(Language, related_name='author_writing_languages')
And inside my 0001_initial file I have a dependencies
dependencies = [
('tibrains_app', '0058_book_history_period'),
]
Could this cause the problem?

Django 3.0.7 - No Changes Detected after making Changes to Model

I am making changes to a model AppContactCnt to add new fields to handle deleting of records.
Here is that models.py
class AppContactCnt(models.Model):
id_cnt = models.AutoField(primary_key=True)
# idcst_cnt = models.IntegerField()
idcst_cnt = models.ForeignKey(AppCustomerCst, models.DO_NOTHING, db_column='idcst_cnt')
idctp_cnt = models.ForeignKey(AppContactTypeCtp, models.DO_NOTHING, db_column='idctp_cnt')
idtrp_cnt = models.IntegerField()
name_cnt = models.CharField(max_length=50)
phone_cnt = models.CharField(max_length=50, blank=True, null=True)
email_cnt = models.CharField(max_length=100, blank=True, null=True)
note_cnt = models.CharField(max_length=100, blank=True, null=True)
receives_emails_cnt = models.BooleanField(blank=True, null=True)
deleted_cnt = models.BooleanField(blank=True, null=True)
# date_deleted_cnt = models.DateTimeField(blank=True, null=True)
# deleted_by_cnt = models.CharField(max_length=100, blank=True, null=True)
class Meta:
db_table = 'app_contact_cnt'
app_label = 'easytrade20'
I tried to add in managed = True no change.
Also tried the following:
Delete all previous migration files and pycache files except init.
python manage.py migrate --fake-initial
python manage.py makemigrations
python manage.py migrate
I am still getting No changes detected
I am not sure how to proceed, any assistance is apprecaited.
You have set app_label = 'easytrade20' in the meta option, but Django is unable to detect an app named easytrade20 in your project. That's why there aren't any changes.
So, add easytrade20 to your INSTALLED_APPS or remove app_label = 'easytrade20' from the model's Meta, and try again to run your migrations.
try this:
python manage.py makemigrations [app_name]
python manage.py migrate
Problem
The app is likely not listed in INSTALLED_APPS located in settings.py, therefore Django has not registered your application and will not detect migrations that need to be run in that application.
Solution
Add easytrade20 to your INSTALLED_APPS and try again to run your migrations.
If the answers above didn't work for you I propose you to make a model and migrations based on an existing database. The steps to complete this task are outlined below.
Below I suggests that your app directory also called easytrade20.
I think that you already have a database that you are working with and all migrations have been applied to it. If not, and saving the
structure of migrations is not critical for you, then skip steps 2-3.
If any of your other apps have migrations that depend on easytrade20 migrations you need to repeat steps 2-5 for every of them (by renaming easytrade20 to your app name) before starting step 6 (and 6+ steps are required for that apps too).
0. Backup your files and database before changes
1. Update your Django version to the latest release
pip install django==3.0.* -U for 3.0.X (3.0.10 is latest release when I write these lines)
or
pip install django==3.1.* for latest one (3.1 have not so much differences from 3.0 and maybe it is right choice for you)
2. Check if all migrations are applied
Use showmigrations command to show applied migrations:
python manage.py showmigrations
You'll see structure with all apps and their migrations like this (app1 and app2 for example):
app1
[X] 0001_initial
app2
[X] 0001_initial
[X] 0002_some_other_migration
You'll see your apps and some django apps/third-party packages, like admin, auth, database, sessions etc.
If you'll see all [X] near every migration - all is fine, go on.
If you'll see some [ ] migration_name - try to apply these migrations first. Run python manage.py migrate app_name migration_name for every unapplied (unchecked) migration top down for every app (app_name for example).
3. Make new models based on your database structure
Use inspectdb to generate models:
python manage.py inspectdb > generated_models.py
File generated_models.py will be created. You need to copy from this file all models that you created for easytrade20 app. Replace your models.py with generated models and remove managed = False at least in the model AppContactCnt.
4. Remove information about easytrade20 migrations from database
python manage.py migrate --fake easytrade20 zero
And now if you'll run python manage.py showmigrations easytrade20 you'll see that all migrations unapplied.
5. Remove your migrations directory
That's it. Remove directory easytrade20/migrations completely.
Then if you'll run python manage.py showmigrations easytrade20 you'll see text (no migrations)
6. Generate new migrations
Run makemigrations command:
python manage.py makemigrations easytrade20
Folder easytrade20/migrations will be created. If not, then you'll need to check two things:
Your settings.py to ensure that easytrade20 included in your INSTALLED_APPS.
Your easytrade20 app settings. Ensure that your files have similar code:
# easytrade20/__init__.py
default_app_config = 'easytrade20.apps.EasyTradeConfig'
# easytrade20/apps.py
from django.apps import AppConfig
class EasyTradeConfig(AppConfig):
name = 'easytrade20'
7. Apply initial migration
If you have database with this table then fake migration:
python manage.py migrate easytrade20 --fake-initial
If not, then run without --fake-initial:
python manage.py migrate easytrade20
8. Make changes to your model, create and apply migrations
Change your models.py file.
Run python manage.py makemigrations easytrade20. New file in your easytrade20/migrations directory will be created.
Run python manage.py migrate easytrade20 to apply new migration.
Make sure your app is registered inside the settings.py, and the INSTALLED_APP section.

HOw to rebuild Django tables?

I was having problems with my dB, so I dropped the tables for one of my Django apps. I then ran makemigrations for the app and tried to migrate. It said everything was up to date. It didn't re-create the dropped tables.
How can I get Django to re-build the tables?
If you only drop your app tables and need to recreate the tables again. You also need to remove all the migrations entry from you app in the django_migrations table.
After you have done that you can run migrate again. Or if you want to reset your migrations files, remove all migrations from that app and run makemigrations and migrate
You have to delete all the files in app/migrations, except __init__.py
project
app1
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
app2
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
...
Then you can run python manage.py makemigrations and python manage.py migrate

Migration issues in python django?

I have MySQL database in backed but when i change any model i does not reflect in backend
python manage.py makemigration app_name
python manage.py migrate
but it show no migrations applied
does mysql does nor support migrations
my model
class blog(models.Model):
name = models.CharField(max_length=10)
try fake migration
python manage.py -fake app_name
Be sure that you already added app_name to INSTALLED_APPS list

Django makemigrations not detecting project/apps/myapp

Solved
Interesting:
./manage.py makemigrations apps.myapp -> "App 'apps.myapp' could
not be found. Is it in INSTALLED_APPS?"
./manage.py makemigrations myapp -> Works.
./manage.py makemigrations -> "No changes detected" (it does detect changes if myapp is in the project root)
Original question:
Project structure
myproject/
myproject/
apps/
myapp/
__init__.py
admin.py
models.py
urls.py
views.py
tests.py
myapp2/
myapp3/
__init__.py
static/
templates/
virtualenv/
manage.py
myproject/apps/myapp/models.py
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=200)
created = models.DateTimeField(auto_now_add=True)
Settings.py
INSTALLED_APPS = [
# ...
'apps.myapp',
'apps.myapp2',
'apps.myapp3',
]
makemigrations cannot find "myapps" that's not in the project root.
At the same time, migrate does find it.
$ ./manage.py makemigrations apps.myapp
App 'apps.myapp' could not be found. Is it in INSTALLED_APPS?
$ ./manage.py migrate apps.myapp
CommandError: App 'apps.myapp' does not have migrations.
Isn't the "let's put our apps into an apps folder" practise valid any more, am I doing something wrong, or is it a bug of the makemigrations command?
Note1: before running makemigrations, I removed the "migrations" folder of myapp, but I'm pretty sure it doesn't matter. It did get re-created when the app was in the project root.
Note2: I did research google and stackoverflow, I only found similar questions where the solution was either "adding the app to settings.py", "running makemigrations before migrate" or "some migration issue between Django 1.6 and 1.7". None of these apply to my situation, I think.
You may have deleted the migrations folder inside the app or __init__.py inside the <app>/migrations/ folder, create a new one
myproject/
apps/
myapp/
migrations/
__init__.py
You can always do makemigrations seperately without doing the above step
python manage.py makemigrations myapp
try
INSTALLED_APPS = [
# ...
'myproject.apps.myapp',
]
and
python manage.py makemigrations myapp
It's works now. It didn't work before because I didn't make any changes to my model.
I'm new to django and was trying to execute the command from the videos I watched. Django documentation tells you that makemigrations create new migrations based on the changes you made to your model. So, I make changes in models.py and admin.py and it's working now.
I had the same problem. I solved it by adding "migrations" file into the app folders.