G
G
gh0sty2019-12-01 19:21:46
PostgreSQL
gh0sty, 2019-12-01 19:21:46

How to resolve "relation does not exist" error in Django?

I am migrating a django project to a server.
It so happened that I have a lot of models. And to get around the 1600 columns limit from PostgreSQL, I split my models into 3 app/model files. Those. I have main, translations and translations2 applications. Each of them runs the django-modeltranslation library . I created 3 databases db, db_translations and db_translations2.
Added them to settings:

INSTALLED_APPS = [
...
    'main',
    'translations',
    'translations2',
...
]

and
DATABASES = {
    'default': {
        ...
        'NAME': 'db',
  ..
    },
    'trans': {
  ...
        'NAME': 'db_translations',
        ...
    },
    'trans2': {
        ...
        'NAME': 'db_translations2',
        ...
    }
}

Ran migrations like:
python3 manage.py makemigrations main
python3 manage.py migrate main
python3 manage.py makemigrations translations
python3 manage.py migrate translations --database=trans
python3 manage.py makemigrations translations2
python3 manage.py migrate translations2 --database=trans2

Executing the update_translation_fields or sync_translation_fields commands from the modeltranslations application . In fact, one of the commands should add translation fields for my languages, in the format field, field_ru, field_en, field_fr.
But an error is thrown :
django.db.utils.ProgrammingError: relation "translations_translations" does not exist
LINE 1: UPDATE "translations_translations" SET "open_ad_ru" = "trans...

So far I haven't found a solution to this problem. When you call runserver, it shows that the translations, translations2 migrations have not really been carried out. (although I explicitly wrote them down in the database, otherwise a restriction error would have been caused)
Tell me please, can I add something to the settings? Already tired of testing commands from the forums)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gh0sty, 2019-12-02
@gh0sty

Okay, I figured it out.

  1. To begin with, we create all our fields for translation, I have about 400 of them.
  2. We count the number of required languages ​​+ 1, I have 6. Why? Modeltranslation leaves the original field in order to quickly access it in the already changed language. Those. dynamic handy feature)
  3. 400 * 6 = 2400 total fields.
  4. We divide these 2400 approximately by 600-800 (if charfield max_length=200). In my example, 5 was enough.
  5. We break our models and translation into 5 (calculated above) applications. This is 600 translation lines, for me, i.e. 100 regular lines (+- 10).
  6. We create databases for these applications (well, this does not need an explanation, create + grant).
  7. We create manual routes in settings, I call them that. Example:
    DATABASES = {
        'default': {  # Сюда будут записываться другие приложения, оставить "default"!!!
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'db',
                'USER': 'username',
                'PASSWORD': 'pass',
                'HOST': 'localhost',
                'PORT': '',
        },
        't': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'db_t',
                'USER': 'username',
                'PASSWORD': 'pass',
                'HOST': 'localhost',
                'PORT': '',
        },
        't2': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'db_t2',
                'USER': 'username',
                'PASSWORD': 'pass',
                'HOST': 'localhost',
                'PORT': '',
        },
        't3': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'db_t3',
                'USER': 'username',
                'PASSWORD': 'pass',
                'HOST': 'localhost',
                'PORT': '',
        },
        't4': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'db_t4',
                'USER': 'username',
                'PASSWORD': 'pass',
                'HOST': 'localhost',
                'PORT': '',
        },
        't5': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'db_t5',
                'USER': 'username',
                'PASSWORD': 'pass',
                'HOST': 'localhost',
                'PORT': '',
        }
    }

  8. We write them all in applications. BUT!!! comment out.
  9. In the migrations folders in each translation application, delete everything except the cache and _init_.
  10. Just in case, we run makemigrations and migrations without parameters for other applications to make sure that everything will be recorded correctly.
  11. Next, uncomment the very first application in settings. And we apply:
    manage.py makemigrations <название приложения перевода>
    manage.py migratе <название приложения перевода> --database=t<номер базы>
    manage.py migratе
    manage.py runserver <параметры>
    *Создаем тестовый экземпляр*
    manage.py update_translation_fields

    And we carry out this set of operations one by one for each application.
    This is done so that there are no crazy mistakes. Believe me, they can be the sea. If something does not work, we recreate the databases (preferably with a new name), clean up the migrations and start over. If something obviously does not work out, we google the error. Here is the main part of all the game that you can meet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question