AppConfig override restricts first makemigration - django

I am overriding the AppConfig and adding below in __init__
default_app_config = 'api.apps.AppnameConfig'
which has some models check I want to create them if doesnot exits all works fine. But when I am deploying this to another machine
python manage.py makemigrations
fails obviously because there are no tables created as there is no migration on fresh project. It is raising ProgrammingError I can do try pass on this but I dont want to go this way.
I also checked if migrations folder exists works fine but again fails on migrate.
Please suggest best way to do this.

Solved it by checking the tables introspection returns [] if no migration is ever applied in __init__
from django.db import connection
if connection.introspection.table_names():
default_app_config = 'pathto.AppnameConfig'

Related

Django makemigrations No changes detected in app

I have trouble with my makemigrations command.
Note: I have successfully make migrations till now, so it is not the first time I try to make migrations on this project.
I have my project in INSTALLED_APPS.
Problem: For some reason project stop detecting any changes in my models.
Inside my project models.py I have:
from myproject.myfolder import myModel1
from myproject.myfolder import myModel2
from myproject.myfolder import myModel3
if a add new models as myModel4 class and import it inside models.py and I try to
python mamange.py makemigrations environment=local
I get No changes detected
I know there are a lot of posts of making the initial migrations, so I even try
python manage.py makemigrations myproject environment=local
I even try to delete all files in __pycache__ but it doesn't work for me.
I even try to delete database and create new one, and it doesn't work either.
EDIT:
Because I delete the database and make it new again, database is empty, but I still get same message.
I just ran into an issue like this. In my case, the problem was that I had installed, through pip, the stable version of the package that I was developing, and Django was importing the stable version rather than my development version. To check if this is the case with you, try adding a syntax error to models.py. If makemigrations doesn't trigger the syntax error, then you'll know that your version is not even being loaded by the python interpreter.
If your model is not inheriting from django model then, you will see aforementioned error. Make sure that your custom model inherits from django models.Model, something like this.
from django.db import models
class Posts(models.Model):
...
Deleting the DB and creating new one will never work since it refer the previous migration files. Delete all previous migration files and pycache files except init. Then try running these.
python manage.py migrate --fake-initial
python manage.py makemigrations
python manage.py migrate
This worked for me

ValueError in Django when running the "python manage.py migrate" command

I needed to add more fields to Django's User model, so I created a custom model class (named Accounts in an app named accounts) that extends Django's AbstractUser class.
After that, I updated my settings.py file, defining the AUTH_USER_MODEL property:
AUTH_USER_MODEL = 'accounts.Accounts'
I then created a migration file for the custom model using the python manage.py makemigrations command.
After that, I ran the python manage.py migrate command and I got this error message:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' isn't installed.
What's the cause of the error and how can I fix it?
UPDATE:
Now, if i run the python manage.py makemigrations command, I get this error message:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' doesn't provide model 'accounts'.
You just delete your previous 0001_initial.py in the migrations folder and try doing the makemigration and migrate again
I have the similar problem. It is the admin app has the cache and migrations history. I solve it by deleting all the cache and migrations history record(pycache file, and 0001.intial etc., keep init.py only) in
YouProject\lib\site-packages\django\contrib\admin\migrations
I too had a similar problem when I changed the name of one of my apps, I had to delete migrations files at two locations, all migrations for the specific app migration folder, then migrations at "Your-project-env/lib/python3.5/site-packages/django/contrib/admin/migrations".
You didn't add accounts to your INSTALLED_APPS. From the comment, I can see accounts.apps.AccountsConfig in your list of apps. Instead of it, just add accounts to your INSTALLED_APPS
It's just because you have already an instance of default user model I think. Start a new project and migrate your models again and it should work.
I have the same problem.
look under your class in models.py, change the app_label to the name of your application, I put the name of the class, and then the error came. doing this I think it fixes the errors.
class Meta:
app_label = 'put_app_name'
Sorry for some grammar error, I'm using google translator.

Django makemigrations wants to delete everything it just created

I just followed this procedure:
makemigrations (success)
migrate (success)
Copy the app on another server (with the migration files)
Create a new empty database on that server
migrate (success, it creates the correct schema)
Fill the new database with data
Just to test: migrate ....
At this point Django says I 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"
But when I run makemigrations, it creates a new one that wants to "Remove field" every foreign key and "Delete model" all of my models. If I run it it empties my database. My models.py are intact.
What is happening ??
I had the same problem in my project. Running forward I can say that django removes models that has no import (making the Delete model migration). The documentation says that you should import your model in the myApp/models/__init__.py file (see https://docs.djangoproject.com/en/1.11/topics/db/models/#organizing-models-in-a-package).
In my case I imported the model somewhere to make manipulations but model had been removing elsewhere.
I had made a useless import in an admin.py file that has solved my situation (I didn't try to follow the documentation and import it in the __init__.py but sure it should help).
I had not realized yet why does it work that way (hope that someone could note this moment) and also hope this solution will help you.
I've just had the same errors, but I hit the makemigrations building a migration full of Remove Field and Delete Model commands before I tried the migrate command.
In my case, the solution was related this being an old project I was resurrecting, and it had a number of Model.Meta.app_name values set, as well as entries in apps.py for each project. These were now in conflict with the way settings was interpreting the project, and even though my models were imported into views and admin, they weren't being seen by the migration code. Deleting these app_name tags on the models, and modifying the AppConfig name in apps.py solved this, so that the running makemigrations again caused the expected changes (alter field, etc), and all was fine.
I didn't find any other questions or answers that quite matched this one and my experience, so I hope if anyone else looks for this, they'll find it here like I did ;-)

How to remove models from django?

In Django how do you remove models that you have synced into the database?
For example in Django tutorial page had this following code
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
Then I used python manage.py sql polls and python manage.py sql choice to create the tables into the database. But what if I did something wrong and don't want that model any more. What's the syntax to remove it?
If you don't want to delete and re-sync your current database, the best way is to drop the table from your model manually:
$ python manage.py dbshell
> (Inside the DB shell)
> DROP TABLE {app-name}_{model-name};
Why not simply try deleting the models from your models.py file? When you run
python manage.py makemigrations
the migrations file should be updated with the deleted models.
There is no syntax. Django doesn't removes tables or columns. You have to manually change your database or use a migration tool like South.
If you justing playing around with tutorial the easier way is to delete your sqlite database file and run a sync again.
Commenting out the class that defines the model did it for me. Once I had done it and ran python manage.py makemigrations,
I got this as response:
- Delete model MyModel.
Checked afterwards with a DB Browser and it was actually removed.
The most easiest solution is to just delete your model from models.py and run
python3 manage.py makemigrations
(Note: Remove the model from everywhere where you have imported it like admin.py, views.py, or any other file where you have imported it)
If you are facing issue to update changes onto DB so you can directly run this command.
python manage.py migrate --run-syncdb
I found a simpler method, by sheer experimentation. Even after deleting tables, Django was not making the migrations, so I did the following:
Simply delete the files created in your myapp->migrations directory, making sure that you do not delete the init.py and pycache
Starting from 001initial.py and downwards delete the files.
Run python manage.py makemigrations
Run python manage.py migrate
-M
Django’s database handling through syncdb is purely additive: any new models will be added, but deleted models will not be deleted and modified models will not be modified.
If you do not have any data you want to preserve, you are fine just dropping and recreating the database: if you have anything you want to preserve, or even if you intend to have anything you want to preserve, I cannot advise you strongly enough to use a migration tool: South has been the de facto standard for every project I’ve worked on.
Since the Migration command handle Model(database) you can do following steps.
First type
python manage.py makemigrations app_name # it will restructure your model
then type
python manage.py migrate app_name # it will apply to restructure your database.
Example:
I had Posts and PostDetail model,
later on, I wanted to remove PostDetail model and some fields(columns) from Posts model too.
I simply run migrations and migrate commands,checked in Mysql Database. It worked fine.
Hope it will work for you too.
Weather you’re removing a single model or a full app, you should first
remove the desired models from the models.py file.
Moreover, you have to make sure that no other file imports these
models or uses them (admin.py, views.py, etc).
Next, run South or migrate your database to properly delete these
models from your database.
Check the source of this information on the link below:
http://www.marinamele.com/how-to-correctly-remove-a-model-or-app-in-django

south migration error app "is not available in this migration"

This problem is basically the same as the previous question
here.
However, the answer there does not work for me. I've installed the trunk version of south, manually entered the import line in the migration file in question, and done a full 'startmigration' in a separate directory and examined the 0001_initial.py file.
I have a Django project with several applications in it, one of them (named 'core') being referred to by the others. The south migration is trying to create a new table, with a column that has a foreign key to a model in core.
I'm currently importing core in the migration in question (0006), and I even added it to migration 0001, although it doesn't seem like that should matter.
Before I do something drastic, like removing that field, running the migration, and adding the field manually, is there a known manual workaround for fixing this south issue?
You probably did not use the --freeze option like this:
python manage.py startmigration <appname> migrate_core --freeze core
Having created a migration like so:
./manage.py startmygration appname --model NewModel
This error occurs:
"The model 'program' from the app 'core' is not available in this migration."
Recreating the migration like this fixes it:
./managepy startmigration appname --model NewModel --freeze core.Program
Just doing "--freeze core" did not do the trick for me.
You can receive this error by trying to access a class that resides in another django app. Check to make sure the class you are trying to access is in the models dictionary.