Adam Starrh
Adam Starrh

Reputation: 6958

Django - "Relation Does Not Exist" on Fresh Migrations

I'm trying to deploy my app, but when running migrate.py for the first time, I get an error explaining that a "relation does not exist". Of course it wouldn't, because I haven't been able to migrate yet!

Looking through the traceback, it seems to be getting tripped up in my managers.py file, but I can't understand why my managers would be getting in the way?

pyto(genius) 00:24 ~/genius (master)$ python manage.py migrate
Traceback (most recent call last):
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "Inventory_purchase" does not exist
LINE 1: ...entory_purchase"."inr_value") AS "inr_value" FROM "Inventory...
                                                             ^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_comm
and_line
    utility.execute()
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/management/base.py", line 342, in execute
    self.check()
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/management/base.py", line 374, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 62, in _run_check
s
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/management/base.py", line 361, in _run_checks
    return checks.run_checks(**kwargs)
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    for pattern in resolver.url_patterns:
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/urls/resolvers.py", line 313, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/cvcexport/genius/Genius/urls.py", line 12, in <module>
    url(r'^', include('Inventory.urls', namespace="Inventory")),
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/cvcexport/genius/Inventory/urls.py", line 3, in <module>
    from Inventory import views
  File "/home/cvcexport/genius/Inventory/views.py", line 67, in <module>
    class SummaryView(InventoryView):
  File "/home/cvcexport/genius/Inventory/views.py", line 70, in SummaryView
    summary_table = build_summary_table()
  File "/home/cvcexport/genius/Inventory/views.py", line 62, in build_summary_table
    table_query = Purchase.inventory.current_summary_data(**kwargs)
  File "/home/cvcexport/genius/Inventory/managers.py", line 31, in current_summary_data
    for item in totals:
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/models/query.py", line 256, in __iter__
    self._fetch_all()
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/models/query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/models/query.py", line 109, in __iter__
    for row in compiler.results_iter():
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 789, in results_iter
    results = self.execute_sql(MULTI)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
    cursor.execute(sql, params)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/cvcexport/.virtualenvs/genius/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "Inventory_purchase" does not exist
LINE 1: ...entory_purchase"."inr_value") AS "inr_value" FROM "Inventory...

My files are kinda bulky, but this is the Manager that is tripping it up:

class EntryManager(Manager):

    def user_create(self, request):
        type = request.POST.get('type', None)
        label = request.POST.get('label', None)
        details = request.POST.get('details', None)
        date_added = date.today()
        sequence = 1 + self.model.objects.filter(type=type, active=True).count()
        label = type + str(sequence) + "- " + label

        entry = self.model.objects.create(
            type=type,
            label=label,
            details=details,
            date_added=date_added,
            sequence=sequence,
            user=request.user
        )

        return entry

    def user_update(self, request, pk):
        entry = self.model.objects.get(id=pk)
        entry.label = entry.type + str(entry.sequence) + "- " + request.POST.get("label")
        entry.details = request.POST.get("details")
        entry.user = request.user

        entry.save()

        return entry

    def user_delete(self, pk):
        entry = self.model.objects.get(id=pk)

        successors = self.model.objects.filter(type=entry.type, active=True, sequence__gt=entry.sequence)
        for obj in successors:
            obj.sequence -= 1
            obj.label = obj.label.split(" ", 1)[1]
            obj.label = obj.type + str(obj.sequence) + "- " + obj.label
            obj.save()

        key = entry.id
        entry.delete()
        return key

    def get_entry_set(self, request):
        """
        Builds selection lists allowing users to link SWOT entries to strategies. Used in swot-matrix.js with
        setStratEntries method.

        Args:
            Request Delivers two relevant variables:
                s_or_w: Indicates "Y-axis" (strength or weakness) requirement.
                o_or_t: Indicates "X-axis" (opportunity or threat) requirement.

        Returns:
            All active entries that match each requested type, packaged in dictionaries and ready for JSON delivery.
        """

        # Build Dictionary of active Strengths or Weaknesses
        s_or_w = request.GET.get("s_or_w")
        strengths_or_weaknesses = self.model.objects.filter(type=s_or_w, active=True)
        s_or_w_data = []
        for entry in strengths_or_weaknesses:
            s_or_w_data.append({
                "id": entry.id,
                "label": entry.label
            })

        # Build Dictionary of active Opportunities or Threats
        o_or_t = request.GET.get("o_or_t")
        opportunities_or_threats = self.model.objects.filter(type=o_or_t, active=True)
        o_or_t_data = []
        for entry in opportunities_or_threats:
            o_or_t_data.append({
                "id": entry.id,
                "label": entry.label,
            })

        # Combine for for JSON delivery.
        data = {
            "s_or_w": s_or_w_data,
            "o_or_t": o_or_t_data
        }

        return data

This is the associated model:

class Entry(GenericObject):
    type = models.CharField(max_length=1, choices=choices.ENTRY_CHOICES)
    sequence = models.IntegerField()
    label = models.CharField(max_length=124)
    actions = managers.EntryManager()
    objects = models.Manager()

    class Meta:
        ordering = ["id"]

Am I breaking the rules somewhere? Why is migrate.py complaining about missing columns on a brand new postgres database?

Upvotes: 2

Views: 1620

Answers (2)

Adam Starrh
Adam Starrh

Reputation: 6958

I never figured out what was wrong. The error would trip at different points in my managers here and there, but would always be triggered first in the views. I simply commented out all of the views and urlpatterns, ran the migrations and then reverted the files.

Upvotes: 1

aman kumar
aman kumar

Reputation: 3156

first migrate that particular app. python manage.py migrate app_name. Which will resolve the dependency.

Upvotes: 1

Related Questions