django model not creating postgresql table - python

I have added a simple model to my reporter app in my django project
class Municipalities(models.Model):
namelsad = models.CharField(max_length=100)
geom = gismodels.MultiPolygonField(srid=4326)
when I python3 manage.py makemigrations reporter
it says no changes detected in reporter
then when I python3 manage.py migrate reporter
Operations to perform:
Apply all migrations: reporter
Running migrations:
No migrations to apply.
but there is no postgresql database table of reporter_municipalities
reporter is included in installed apps
Municipalities model is in the model.py file in reporter app
I should add I had a counties table and manually deleted it in postgresql and tried adding the municipalities model to create a table
municipalities is also in the django_content_type
but there is no table of municipalities
update
Changed Class Municipalities to Class Muni
python3 manage.py makemigrations reporter
then it asked me
Did you rename the reporter.Municipalities model to Muni? [y/N]
if I click y
then run migrate
gives me
django.db.utils.ProgrammingError: table "reporter_municipalities" does not exist
but I am so confused that table does not and never existed!!
I cannot migrate at all to the DB now because of "reporter_municipalities"

When Django looks for migrations, it searches for the app reporter as is it configured in your INSTALLED_APPS. It uses AppConfig or application name to retrieve the full package name, ie my_project.reporter. This package must be available in your PYTHONPATH. It should be available only in you development project, but could happen that it is "installed" in your virtualenv. It can happen running pip install . (without -e) or (with some configurations) running tests (I have seen this happen with tox).
In this scenario you have two my_project.reporter available to python, but you edit/update the "second" one. When you run ./manage.py makemigrations, python first find the code that you did not change (the installed one in your virtualenv), so it does not find the updates.
To check if you have this duplication, you can:
uninstall your project (pip uninstall <django-project>) to see if it refers to a "symbolic link"
If this is the case you will see something like
Uninstalling <PROJECT>:
Would remove:
/data/PROGETTI/UNICEF/odk/.venv/lib/python3.6/site-packages/<PROJECT>.egg-link
Proceed (y/n)? y
Note the egg-link at the end of the line.
OR
open a shell and type import <full_path_of_reporter> AS pkg; print(pkg.__file__) and check the filepath

Sorry I can't give a very technical answer but I got round this my creating a table with the same name directly via pgAdmin. Making and running migrations. At this point the table was recognised but obviously didn't work correctly because it had no columns. I then commented out the model in models.py and made ran migrations which removed the table. I checked on pgAdmin that the table had been removed by this point. I then deleted the migrations files (no idea if this was necessary), uncommented the model in models.py and then made and ran migrations again. Worked fine after that.

I'm late to the question but for people facing this in future, there are files inside the pycache folder inside the migrations folder with .pyc extension. Just delete those files if they are already there and run from starting, i.e., start from makemigrations again and hopefully you'll get it. I got it the same way by deleting the files in migrations folder other than init and deleting the files inside pycache folder other than init.

ty running python ./manage.py makemigrations "app_name" as the last argument, then the migrate command. this worked for me when having the same issue.

Related

Uninstalling django-tinymce. Import error when makemigrations

Thanks for taking the time to read this.
I was not happy with TinyMCE extension in django and decided to switch to django-summernote. I first ran
pip uninstall django-tinymce
Removed all mentions of tinymce in the actual project.
Followed the instruction to install django-summernote. Upon completion I decided to run
python manage.py makemigrations
python manage.py migrate
to apply new extension, but get an error:
File "/Users/rasulkireev/Sites/dj-pw/now/migrations/0006_auto_20190703_0134.py", line 4, in <module>
import tinymce.models
ModuleNotFoundError: No module named 'tinymce'
I'm not sure why would Django care about what I did previously since I am simply asking to replace the editor. I can't run python manage.py runserver either.
I can't do anything before fixing this. I beg you guys, please help.
Note: I would ideally want to keep the contents of my current database.
Thanks for taking the time to read this.
Once you have removed all the mentions of tinymce from the models, admins and other files, you need to migrate using a special "flag":
python manage.py makemigrations
python manage.py migrate --fake-initial
I am not to clear on the backend of this method, but it works. You can read more about it the official Django website:
https://docs.djangoproject.com/en/2.2/topics/migrations/
This will make a new initial migration for your app. Now, run python
manage.py migrate --fake-initial, and Django will detect that you have
an initial migration and that the tables it wants to create already
exist, and will mark the migration as already applied. (Without the
migrate --fake-initial flag, the command would error out because the
tables it wants to create already exist.)
Note that this only works given two things:
You have not changed your models since you made their tables. For
migrations to work, you must make the initial migration first and
then make changes, as Django compares changes against migration files, not the database.
You have not manually edited your database - Django won’t be able to detect that your database doesn’t match your models, you’ll just get errors when migrations try to modify those tables.

Python social auth in Django, makemigrations detects no changes

I was following the documentation to get python social auth on my django project
https://python-social-auth.readthedocs.org/en/latest/configuration/django.html
And after adding 'social.apps.django_app.default', to the INSTALLED_APPS in my settings.py I run this:
python manage.py makemigrations
I get this
No changes detected
Shouldn't this command be doing something. Because without this I can't migrate to create the tables that are needed for the auth.
EDIT:
I've also tried this command, and still ended up getting the same result
python manage.py makemigrations main
where 'main' is the name of my app
Today i ran into this problem. The error is in the documentation itself.
You should run $ python manage.py migrate directly. It creates tables in the database.
All the old tutorials used makemigrations, I think it was used in earlier versions of django.
My answer will be cover some basics so one can understand this types of errors easily.
Let me clear some basic terminology about migrations in the latest versions of Django(From 1.7 to under development ones).
In older versions of Django when One has to make changes in Models (Eventually in Database) then One has to use South app to apply changes in Database without affecting older database.
Django developer community has included this south app in and after Django 1.7 and also provided some simple commands to apply migrations.
When one install New app(Above question scenario) or one make changes in existing models and wish to apply changes in database then one has to tell database about what changes they want to make. To do so one has to make migrations and below is the command.
$ python manage.py makemigrations app_name
or If it is initial then no need to specify app_name, it will consider all apps.
This command will generate migrations files which will include instructions for database to make what tables and what are the attributes of that table and what will be the relationships between tables and what are changes in the current tables etc etc.
Now one has to run below command to apply this all changes in database.
$ python manage.py migrate app_name
or If it is initial then no need to specify app_name.
Please shoot any questions If you have any and please take a look at Django's official documentation on migrations for more information.
The possible reason is your project doesn't use any database models of 'social' application yet. Add a URL in your urls.py, link it to 'social' urls.
to Django < 1.8
INSTALLED_APPS ['social.apps.Django_appConfig',]

Django 1.7 Migrations issue with my old project

I have a django 1.5 app , now I migrated it to django 1.7 and app is working fine with old database. But Now I want create migrations for that app using django 1.7.
I deleted old migrations and just kept migrations folders with __ init__ files
then I ran ./manage.py makemigrations
while running ./manage.py migrate its giving some errors.
django.db.utils.ProgrammingError: relation "django_site" does not exist
LINE 1: SELECT (1) AS "a" FROM "django_site" LIMIT 1
I have added django_sites to installed apps.
Is there any particular way to create/apply migrations when updating to native migrations?
you have a migration that depend on django.contrib.site but is the tables related are not available.
you can
move django.contrib.site up in the INSTALLED_APPS
check the attribute dependencies of your migration
Anyway I think your problem is with the default value of some field.
If you don't find the involved application you can:
disable all the apps in the INSTALLED_APPS
enable the first one
run makemigrations
delete all migrations
enable the next application
run makemigrations
... repeat step 4..6 until you get the error
From the documentation:
If you already have pre-existing migrations created with South, then the upgrade process to use django.db.migrations is quite simple:
Ensure all installs are fully up-to-date with their migrations.
Remove 'south' from INSTALLED_APPS.
Delete all your (numbered) migration files, but not the directory or init.py - make sure you remove the .pyc files too.
Run python manage.py makemigrations. Django should see the empty migration directories and make new initial migrations in the new format.
Run python manage.py migrate. Django will see that the tables for the initial migrations already exist and mark them as applied without running them.
I suspect you forgot to
find . -iname "*.pyc" | xargs rm

South cannot find my models

I'm having trouble trying to add south to an existing django project. Here is my situation:
My project is called alerts. Here is my folder structure:
djangoprojects/
alerts/
manage.py
alerts/
__init__.py
urls.py
views.py
models.py
wsgi.py
settings.py
common/
__init__.py
models/
__init__.py
models.py
The file alerts/alerts/models.py has the following content:
from common.models.models import *
In other words, the only thing that alerts/alerts/models.py does is import the "real" models from alerts/common/models/models.py, which has a large number of models that were created from a past project.
I also have a MYSQL database that I'll call 'existing_database'. This is the database that corresponded to the models on my past project, which ran on another machine. On that other machine I used mysqldump to get the sql file for this database and copied it over to my new machine. Then I used mysql directly to create existing_database on my new machine. So now the database existing_database should match the models in alerts/common/models/models.py.
So next I went through the documentation to try to add south to the alerts project. This is what I did:
I added 'south' and 'alerts' to the INSTALLED_APPS inside alerts/alerts/settings.py. I'm not sure why I have to add 'alerts' to settings.py inside the alerts project - you'd think the fact that the settings file was in the project folder would be enough. But it complained if I didn't add it explicitly, so I did.
I cd-ed to djangoprojects/alerts and ran ./manage.py syncdb. It said it synced some stuff, including south and alerts
Then I ran ./manage.py convert_to_south alerts. This is where I ran into problems. It said:
This application has no models; this command is for applications that already have models syncdb'd.
Make some models, and then use ./manage.py schemamigration alerts --initial instead.
So I tried running ./manage.py schemamigration alerts --initial. It created an initial migration but that migration files has almost nothing in it. All it has is a class Migration. None of my real models are anywhere to be found.
To see if it can recognize any new models, I went to common/models/models.py and added a dummy model. Then I ran ./manage.py schemamigration alerts --auto and the response I got back was "Nothing seems to have changed". So it apperas it still knows nothing about my real models.
Ao at this point I'm totally confused. Like I said, I have an existing database with real data, and I have an existing set of models code that should exactly represent that actual table structure. I just want to start using this in my alerts project and I need south because I will be adding and changing models.
How do I accomplish this?

How to handle refactor with south (django)?

I have installed south and make some migrations. Now there's a 'migrations' directory in the folder app. My question is: when I am refactoring models, which entries in the migration directory files I must apply the changes? I think some entries are related directly with the database schema, and others with the code itself. I couldn't fina an answer to this in the south docs.
Make the changes to your models then run python manage.py schemamigration yourapp --auto. This will create the migrations for you (you'll see a new file in your migrations directory every time you do this process).
Sometimes you really need to edit a migration manually, but you should try and avoid it. Particularly if you have already run the migration (the south app keeps a record of which migrations have been run so it knows the state of your database).
South is designed to support moving between different versions of your code without breaking your database. Each migration file in the migrations directory represents a snapshot of your code (specifically a snapshot of your models.py). You migrate from version to version by running python manage.py migrate yourapp version_no

Categories

Resources