21

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.

10
  • 5
    did you run makemigrations with the app name ? Commented Sep 24, 2017 at 14:56
  • 2
    ok, the model ClubOfficial is stored in which file ? Commented Sep 24, 2017 at 15:12
  • 3
    sorry for the silly questions. Have you already checked the last files in the migrations folder ? Commented Sep 24, 2017 at 15:19
  • 3
    check inside 0001_initial, do you see the model ClubOfficial ? Commented Sep 24, 2017 at 15:28
  • 2
    @user3257736 what is the result of python manage.py showmigrations? is there a tick before 0001_initial of your app? Commented Sep 24, 2017 at 16:13

8 Answers 8

31

TL;DR: Make sure your app's migrations folder has an __init__.py file. If it isn't there, create it again as an empty file.

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.

Sign up to request clarification or add additional context in comments.

8 Comments

adding init,py under migrations folder did the job for me. Thanks.
Yep, worked for me too. My issue was the apps.config wasn't named correctly so the migration wasn't being created. After renaming, I added the migrations folder with an init.py, ran makemigrations, then migrate. Thanks
Don't delete migration files, it's a very bad idea. if you don't know what you're doing. Or you will have a headache like me.
@Sergo that's exactly what I did lol
@Sergo even worse than yours, it ends with 'ego'
|
7

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

$ python manage.py makemigrations

and

$ python manage.py migrate

Comments

1

In case you have deleted your migration folder. Create a folder called migration in whatever app name you have created and then create a file in the migration folder called __init__.py

Comments

1

Another method to fix relation does not exist error

  • Create same table in db(postgre, mysql) using sql query tool
  • now comment your model in models.py and admin.py
  • run migration using :
  • python manage.py makemigrations app_name
  • python manage.py migrate
  • now uncomment and run migrations command again

I encountered same issue and fixed using following method, I am using postgres(pgadmin 4).

Comments

1

This can happen when you delete the app and then create it again. So, you may have orphaned database tables in your database that are associated with the old version of the app.

try the following:

python manage.py migrate --fake app_name zero
python manage.py makemigrations app_name
python manage.py migrate

Comments

0

In my case I was pointing to a different databases between my local server and the production server. The database that the production server was pointing to was a few versions behind, so the server could not locate the relation. If your issue were localized to one environment, check the configs first.

Comments

0
  1. You have to make sure that the migration takes place. So:

  2. Add the application name to the command lines and check for creation or change of files /0001_initial.py (0001 represents the order of the file created)

  3. For exemple: if your application is called appsehalcia and your environment sehalcia_env, AND you want to create the three models department, person and division do:

    (sehalcia_env) ... /appsehalcia# python manage.py makemigrations appsehalcia

Observe something like this:

Migrations for 'appsehalcia':
appsehalcia/migrations/0001_initial.py - Create model departement - Create model person - Create model division

Then type:

(sehalcia_env) .../appsehalcia# python manage.py migrate appsehalcia

Observe something like this:

Operations to perform:

Apply all migrations: appsehalcia

Running migrations:

Applying appsehalcia.0001_initial...OK

Comments

0

You need to go through migrations file manually and edit it to make migration possible.

In my case I have the following error Applying bot.0008_alter_contenttext_pagename...Traceback (most recent call last): File "/home/uby/virtualenv/ecoregister_main/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) psycopg2.errors.StringDataRightTruncation: value too long for type character varying(25)

this is because of the following content of the migration:

# Generated by Django 3.2.19 on 2023-07-12 13:17

dependencies = [
    ('bot', '0007_contenttext_pagename'),
]

operations = [
    migrations.AlterField(
        model_name='contenttext',
        name='pagename',
        field=models.CharField(default='none', max_length=25),
    ),
]

It means that I was trying to put lengthy string into charfield of 25 chars. I just put length of 250 chars and do python manage.py migrate

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.