5

I developed a Django application deployed on DigitalOcean's Ubuntu server with Postgres db. Everything worked fine, without any problems, but today after adding new model, I'm getting this error: relation "documents_app_document" does not exist although I have this model, where some of my models inherits from Document model. But somehow it was deleted from database, and now I can't add it back to database after migration. How can I add that model as a table again to database ?

p.s: But I've opened my migration file named '0001_initial.py', there is migrations.CreateModel( name='Document'...

models.py:

class Document(models.Model):
    created_date = models.DateTimeField(default=timezone.now, blank=True, null=True)
    added_by = CurrentUserField()
    purpose = models.CharField(blank=True, max_length=300, null=True)


    def __str__(self):
        return str(self.added_by)

class MedicalDocument(Document):
    policy_number = models.CharField(max_length=20, blank=True, null=True)
    medical_institution = models.CharField(max_length=100, blank=True, null=True) 

Migration error:

Operations to perform:
  Unapply all migrations: documents_app
Running migrations:
  Rendering model states... DONE
  Unapplying documents_app.0001_initial...Traceback (most recent call last):
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "documents_app_document" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in migrate
    state = self._migrate_all_backwards(plan, full_plan, fake=fake)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 196, in _migrate_all_backwards
    self.unapply_migration(states[migration], migration, fake=fake)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/executor.py", line 269, in unapply_migration
    state = migration.unapply(state, schema_editor)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/migration.py", line 175, in unapply
    operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 120, in database_backwards
    schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 487, in remove_field
    self.execute(sql)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/azersigorta/insuranceproject/env/lib/python3.5/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "documents_app_document" does not exist
4
  • 1
    After you created the new model, did you run makemigrations and migrate? Commented Dec 20, 2020 at 17:45
  • It would also help to actually add the code of the models and maybe even of the migration(s). Commented Dec 20, 2020 at 17:46
  • @GlennDJ, yeah, I did, for sure. During migration this error happens. Okay, I'm adding Commented Dec 20, 2020 at 17:53
  • You don't seem to have a migration that creates the model MedicalDocument. If there's no migration that has this, running makemigrations should create a new migration including it. Commented Dec 20, 2020 at 18:06

4 Answers 4

7

After adding changing / adding a new model, always make sure to run python manage.py makemigrations and python manage.py migrate. If for any reason (migration tree re-arrangement, database failure etc.) something went wrong, you can reverse to a specific migration by doing python manage.py migrate {app_name} {migration_index}. So what I would suggest in your situation is that you try python manage.py migrate {app_name} zero, and then re-migrate back to the latest version. You might also need to use --fake.

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

8 Comments

I've already did migrations. The command python manage.py migrate {app_name} zero resulted for me again with the same error.
@BobReynolds Did you try doing it with --fake, and then re-applying it? Sounds like the database state doesn't match the Django migrations state, and using --fake should definitely solve it.
Please look at my full error report, maybe you'll find some hint. About fake I'll try now. So, what should I do after fake migration ?
I did have a look, please try with fake. Always works for me and this is pretty much what it's made for. After faking, you should simply run python manage.py migrate without fake, and everything should work fine.
It worked, man. But on my website (production) still displaying relation "documents_app_document" does not exist
|
5

I faced this error, I surfed whole Stackoverflow, I just try python(3) manage.py makemigrations --help, and then I saw --skip-checks and i just tried, It was amazing, It woked. I was trying to solve this error about 2 hours.

  1. python(3) manage.py makemigrations --skip-checks
  2. python(3) manage.py migration --skip-checks
  3. python(3) manage.py runserver

And you'll see your localhost

The next times you just run the three commands without --skip-checks I knew one thing that one of the best fixing is docs.

Comments

0

Here is a possible workaround: Delete old migrations. Comment out all fields in all your models that relates to Document model and perform makemigrations and migrate to create 'Document' table alone. Uncomment fields related to Document in other models and make migrations again.

Comments

0

I ran into the (seemingly) same problem. Could not run migrations or anything…

The reason in my case is :

I have 2 Databases (with a DB-Router). In one model of Database2 I use a function for a CHOICES-generation witch uses data from model-X of Database1. I wrote the function after the Creation of Database-1.Model-X.

When I now try to deploy the new migrations on the production system without the new Database-1.Model-X I get the same Error.

I saw 3 possibilities:

  • commenting out the function accessing the not existing Model before applying the migrations (somehow lame and error-prone)
  • move the choices generation away from the model to the form (didn't changed anything)
  • changing/extending the Database-Router to allow ForeignKey-Relations between databases, using a ForeinKey-Relation to DataBase1 while selecting the needed (unique) field in the ForeignKey definition as to_field="XYZ" so the content of the Database2-Field is filled with the right data. <- This 'works for me'™

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.