2

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

enter image description here

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"

7
  • Did you included the app in INSTALLED_APPS? Commented Oct 5, 2018 at 18:39
  • yes it is in installed apps Commented Oct 5, 2018 at 18:39
  • What file did you declare the model Municipalities in? Commented Oct 5, 2018 at 18:43
  • @RedCricket model.py Commented Oct 5, 2018 at 18:44
  • Would you mind deleting your migrations directory from reporter app, and then do makemigrations and migrate respectively? Commented Oct 6, 2018 at 5:51

4 Answers 4

1

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:

  1. 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

  1. open a shell and type import <full_path_of_reporter> AS pkg; print(pkg.__file__) and check the filepath
Sign up to request clarification or add additional context in comments.

3 Comments

I am also not sure what this would accomplish. It's not any installed pip app, it's a model within the Django project.
okay I will have to wait a few days until I am back at work, your explanation is a bit over my head. and I will add that my project is not in a virtual env
I couldn't figure this out -- luckily I was not so far into my project. I ended up switching over to a new postgres DB
1

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.