13
votes

I know there are many questions about this problem, I looked through the solutions and unfortunately none of them worked for me.

I created a new app called "usermanagement", and added a model to the app. After adding the model I added usermanagement to INSTALLED_APPS in settings. Then I ran python manage.py makemigrations, and python manage.py migrate. This all worked fine! I also did try running the migrations with the app-name.

The problems start when I try to add a new instance of the model to the database in the Python-Django shell, by using:

>>>a = ClubOfficial(name="randomName", email="[email protected]")
>>>a.save()

I get the following error:

django.db.utils.ProgrammingError: relation "usermanagement_clubofficial" does not exist LINE 1: INSERT INTO "usermanagement_clubofficial" ("name", "email") ...

Below is the model code:

class ClubOfficial(models.Model):

    name = models.CharField(max_length=254)
    email = models.EmailField(max_length=254)

If it helps, I use postgresql, and have tried restarting the server. The other apps in the program also work perfectly fine, it is just usermanagemenet that has this problem.

Does anyone know what could be going wrong?

Thank you for your time!

Note: I created a new app now with a different name, copy-pasted things from usermanagement and everything worked fine. I think the problem might be that before there was an app named usermanagement which was deleted, before I created it again. Maybe that messed up the database somehow.

2
did you run makemigrations with the app name ?PRMoureu
ok, the model ClubOfficial is stored in which file ?PRMoureu
sorry for the silly questions. Have you already checked the last files in the migrations folder ?PRMoureu
check inside 0001_initial, do you see the model ClubOfficial ?PRMoureu
@user3257736 what is the result of python manage.py showmigrations? is there a tick before 0001_initial of your app?Jahongir Rahmonov

2 Answers

15
votes

I ran into this. In my case I had a previously working django app, not yet moved to production, so I deleted everything in my app's migrations folder, then using django extensions I wiped the postgresql database and cached files with:

./manage.py clear_cache
./manage.py clean_pyc
./manage.py reset_schema
./manage.py reset_db
# then deleted all files (including __init__.py) from my app's migrations folder.

I verified that my postgresql database had no tables. I then ran:

./manage.py makemigrations
./manage.py migrate

Which gave the output:

No changes detected
./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  (about 11 more lines of output here which are similar)

It is notable that my model's names where nowhere in the migration. I printed the tables in my postgresql database and the Widget and Section tables were both missing. Running the app gave me this error (I substituted 'app' and 'model' for their real names):

ProgrammingError at /my_path
relation "app_model" does not exist
LINE 1: ..."."my_field", "app_model"."my_field" FROM "appname...

So the tables for the models were not being created in the database. My app's migrations folder was also completely empty. The solution was to just add an empty __init__.py to my app's migrations folder, and after that running makemigrations was able to create and save the migration file in the folder. This could then be executed with migrate. You can test this for yourself by running makemigrations with and without the __init__.py in the migrations/ folder.

This solution was not mine but one given by user Ljubitel on another post but it was not the accepted answer there, but the accepted answer didn't work for me so I wrote this solution here to hopefully help others.

1
votes

I had this same problem, but all I had to do was run

$ python manage.py makemigraions

and

$ python manage.py migrate