20

I am attempting to set up a website on cookeicutter, I created a new app called "bots" and added a class called Trade within models that lists 2 parameters, "titles" and "units". After migrating and running the server, when I open the admin panel and click on the "+ add" button within the panel to create a trade (see picture). The Django Webpage returns this error:

django.db.utils.ProgrammingError: relation "bot_trade" does not exist
LINE 1: ...."id", "bots_unit"."sell", "bots_unit"."buy" FROM "bots_unit...

Additonal Info: Running my django within a docker with postgreSQL

pic of admin panel

Models.py

from django.db import models
from datetime import date
#from django.contrib.auth.models import AbstractUser
#from .models import User
from django.urls import reverse
from django.urls import reverse_lazy
from django.conf import settings
import uuid


class Unit(models.Model):

TRADE_UNIT = (
    ('ETH', 'Ethereum'),
    ('BTC', 'Bitcoin'),
    ('LTC', 'Litecoin'),
    ('IOT', 'IOTA'),
    ('OMG', 'OmiseGo'),
    ('BCH', 'BitcoinCash'),

)

sell = models.CharField(max_length=3, choices=TRADE_UNIT, blank=True, default='ETH', help_text='Currency to Sell')
buy = models.CharField(max_length=3, choices=TRADE_UNIT, blank=True, default='BTC', help_text='Currency to Buy')

def get_absolute_url(self):
    """
    Returns the url to access a particular author instance.
    """
    return reverse('unit-detail', args=[str(self.id)])

def __str__(self):
    """
    String for representing the Model object.
    """
    return '%s, %s' % (self.sell, self.buy)

class Meta:
    db_table = 'bots_unit'
    ordering = ['sell','buy']


class Trade(models.Model):
title = models.CharField(max_length=200)
unit = models.ForeignKey('Unit', on_delete=models.SET_NULL, null=True)


def __str__(self):
    """
    String for representing the Model object.
    """
    return self.title

def get_absolute_url(self):
    """
    Returns the url to access a particular book instance.
    """
    return reverse('trade-detail', args=[str(self.id)])

class Meta:
    db_table = 'bots_trade'


class TradeInstance(models.Model):
"""
Model representing a specific copy of a book (i.e. that can be borrowed from the library).
"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text="Unique ID for this particular trade across whole database")
trade = models.ForeignKey('Trade', on_delete=models.SET_NULL, null=True)
amount = models.CharField(max_length=200)
price = models.CharField(max_length=200)
imprint = models.CharField(max_length=200)
time_initiated = models.DateTimeField(null=True, blank=True)
#initiator = models.ForeignKey(AbstractUser, 
on_delete=models.SET_NULL, null=True, blank=True)

position_status = (
    ('L', 'Long'),
    ('S', 'Short'),
)

position = models.CharField(max_length=1, choices=position_status, blank=True, default='L', help_text='Order Type')

class Meta:
    ordering = ["position"]


def __str__(self):
    """
    String for representing the Model object
    """
    return '%s (%s)' % (self.id,self.trade.title)

Admin.py

from django.contrib import admin
from .models import Trade, TradeInstance, Unit



# Define the admin class
@admin.register(Trade)
class TradeAdmin(admin.ModelAdmin):
    pass


@admin.register(Unit)
class UnitAdmin(admin.ModelAdmin):
    pass

UPDATE1: I deleted the content inside the migrations folder a few times, but this is what is currently inside of '0001.initial.py' after running 'makemigrations' and 'migrate':

# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2017-10-12 17:55
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Trade',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=200)),
            ],
        ),
        migrations.CreateModel(
            name='TradeInstance',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, help_text='Unique ID for this particular trade across whole database', primary_key=True, serialize=False)),
                ('amount', models.CharField(max_length=200)),
                ('price', models.CharField(max_length=200)),
                ('imprint', models.CharField(max_length=200)),
                ('time_initiated', models.DateTimeField(blank=True, null=True)),
                ('position', models.CharField(blank=True, choices=[('L', 'Long'), ('S', 'Short')], default='L', help_text='Order Type', max_length=1)),
                ('trade', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='bots.Trade')),
            ],
            options={
                'ordering': ['position'],
            },
        ),
        migrations.CreateModel(
            name='Unit',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('sell', models.CharField(blank=True, choices=[('ETH', 'Ethereum'), ('BTC', 'Bitcoin'), ('LTC', 'Litecoin'), ('IOT', 'IOTA'), ('OMG', 'OmiseGo'), ('BCH', 'BitcoinCash')], default='ETH', help_text='Currency to Sell', max_length=3)),
                ('buy', models.CharField(blank=True, choices=[('ETH', 'Ethereum'), ('BTC', 'Bitcoin'), ('LTC', 'Litecoin'), ('IOT', 'IOTA'), ('OMG', 'OmiseGo'), ('BCH', 'BitcoinCash')], default='BTC', help_text='Currency to Buy', max_length=3)),
            ],
            options={
                'ordering': ['sell', 'buy'],
            },
        ),
        migrations.AddField(
            model_name='trade',
            name='unit',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='bots.Unit'),
        ),
    ]

When I run 'showmigrations':

dominic@dom-Inspiron-7559:~/Desktop/Projects/vickibot/vicki$ docker-compose -focal.yml run django python manage.py showmigrations
Postgres is up - continuing...
account
 [X] 0001_initial
 [X] 0002_email_max_length
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
bots
 [X] 0001_initial
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique
 [X] 0003_set_site_domain_and_name
socialaccount
 [X] 0001_initial
 [X] 0002_token_max_lengths
 [X] 0003_extra_data_default_dict
users
 [X] 0001_initial

UPDATE2:

'manage.py migrate --fake bots zero' output:

dominic@dom-Inspiron-7559:~/Desktop/Projects/vickibot/vicki$ **docker-compose -f local.yml run django python manage.py migrate --fake bots zero**
Postgres is up - continuing...
Operations to perform:
  Unapply all migrations: bots
Running migrations:
  Rendering model states... DONE
  Unapplying bots.0001_initial... FAKED

'manage.py migrate bots' output:

dominic@dom-Inspiron-7559:~/Desktop/Projects/vickibot/vicki$ docker-compose -f local.yml run django python manage.py migrate bots
Postgres is up - continuing...
Operations to perform:
  Apply all migrations: bots
Running migrations:
  Applying bots.0001_initial... OK
Dominic M.
  • 873
  • 3
  • 17
  • 34

4 Answers4

44

You probably haven't created any migrations for your bot app. You need to specify the app name to create the initial migrations:

./manage.py makemigrations bot

Then run migrate to run the migration and create the missing table:

./manage migrate

When you run showmigrations, you can see that Django thinks that it has already applied the initial migration for your bots app. This could be because you ran --fake for that app.

bots
 [X] 0001_initial

You can tell Django to mark the migrations as unapplied, then rerun the migration with:

manage.py migrate --fake bots zero
manage.py migrate bots

This should work, as long as no tables from the bots app have been created yet. If only some of the tables have been created, then fixing up the database will be much trickier.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Hi Alasdair, I've done this already. I tried migrate --fake as well, thanks for the reply though. – Dominic M. Oct 12 '17 at 02:12
  • Doing fake might have been the problem - it marks the migration as done without actually applying it. – Alasdair Oct 12 '17 at 07:24
  • I think you'll need to provide more information if you want help. What do your migrations look like? What does `manage.py showmigrations` output? Do the tables for trade and unit appear in the database? Do you get the same error when you try to view the admin page for `Unit`? – Alasdair Oct 12 '17 at 07:39
  • Okay, so I added 'showmigrations' and '0001.intial.py' from my migrations folder to my original post under UPDATE1. To your question about Unit, it does work! it allows me to select the 'buy' and 'sell' fields', but when I hit 'save' it returns a very similiar error as follows: django.db.utils.ProgrammingError: relation "bots_unit" does not exist LINE 1: INSERT INTO "bots_unit" ("sell", "buy") VALUES ('ETH', 'BTC'... – Dominic M. Oct 12 '17 at 18:07
  • 2
    What is the output if you run `manage.py migrate --fake bots zero` to mark the bots migrations as unapplied, then `manage.py migrate bots` to re-run the migrations? – Alasdair Oct 12 '17 at 19:25
  • IT WORKED! So --fake app zero removes all previous migrations for that specified app. Thank you so much dude – Dominic M. Oct 12 '17 at 20:14
  • 2
    `--fake zero` doesn't remove the migrations - it just marks the `bots` migrations as unapplied. That means that when you run `migrate`, Django will apply those migrations, and create the missing tables. – Alasdair Oct 12 '17 at 20:34
  • So... the problem is still hidden somewhere in my code and will create missing tables – Dominic M. Oct 12 '17 at 20:59
  • 1
    I don't understand your last comment. I don't think you've got a problem in your code. You probably ran `manage.py migrate --fake` which meant that your database got out of sync with your migrations table. Faking back to `bots zero` fixed the issue. – Alasdair Oct 12 '17 at 21:50
  • 1
    I would like to know why you need to add the app name, and manage.py makemigrations is not enough. Adding the app name solved my problem. I think you should explain why in your answer. – mfnx Jun 20 '20 at 06:55
5

Another reason why you might face the same issue is because you're trying to access datas earlier than creating them in your database. For example I wasn't able to perform migrations because of this line of code:

cat_choices = [(cat, cat) for cat in Category.objects.all().values_list('title', flat=True)]

the problem is:

Category.objects.all().values_list()

The problem is that the code is trying to grab the values from model Category before the db table for category even exist. For this reason it wasn't able to perform makemigrations and migrate. To solve this you can enclose the code in try except block with ProgrammingError and OperationalError as the exception.

try:
    cat_choices = Category.objects.all().values_list('title')
except (OperationalError, ProgrammingError) as e:
    cat_choices=[]
Nirjal Mahat
  • 247
  • 3
  • 5
4

I had a problem, and I really struggled to figure out what was wrong. I made an application with the Django framework. Everything worked fine on my local machine, including the DB. Only, when I put my application on the server, I wanted to migrate the database, but I had this error :

Traceback (most recent call last):
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "administration_parks" does not exist
LINE 1: ...name", "administration_parks"."availability" FROM "administr...
                                                             ^


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/thomas/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 75, in handle
    self.check(databases=[database])
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 419, in check
    all_issues = checks.run_checks(
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/checks/registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/resolvers.py", line 412, in check
    for pattern in self.url_patterns:
  File "/home/thomas/env/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/resolvers.py", line 598, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/thomas/env/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/thomas/AnimauBoue/AnimauBoue/urls.py", line 21, in <module>
    url(r'^', include('administration.urls')),
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/thomas/AnimauBoue/administration/urls.py", line 4, in <module>
    from .views import index
  File "/home/thomas/AnimauBoue/administration/views.py", line 12, in <module>
    from .forms import ConnectionForm, UpdateDataForm, AddClientForm, SelectParkAndClientForm, DogForm, AddDog
  File "/home/thomas/AnimauBoue/administration/forms.py", line 26, in <module>
    class SelectParkAndClientForm(forms.Form):
  File "/home/thomas/AnimauBoue/administration/forms.py", line 31, in SelectParkAndClientForm
    for park in parks:
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/query.py", line 280, in __iter__
    self._fetch_all()
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "administration_parks" does not exist
LINE 1: ...name", "administration_parks"."availability" FROM "administr...

In fact, I came to understand that Django, when it migrates a DB, seems to perform some manipulation on views from urls.py of applications. And in my case, this was the problem. So the solution was:

  1. Comment all my url patterns, and then do the migrations, and there it worked !

  2. At this stage, it allowed me to create the tables present in my models. But Django's default tables weren't created since I no longer had any url schema. So I uncommented all the urls in urls.py, do a new pull on my server, and redo the migrations. And there, all the tables created by default by django have been created.

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
  • 1
    Wow, thanks! It also worked for me too. I had to comment out url patterns **and also an import**: `import admin_app.views as admin_views` – Konstantin Kozirev Feb 04 '22 at 15:24
0

In case anyone else came across this question when looking for django.db.utils.ProgrammingError, be aware, that this question concerns does not exists issue. If you are looking for error relation already exists, you can find this question in link django.db.utils.ProgrammingError: relation already exists

And nice explanation can be found here https://dev.to/siumhossain/djangodbutilsprogrammingerror-column-of-relation-appnametable-already-exists-231g

NelliaS
  • 29
  • 4