2017-10-11 47 views
0

我試圖在cookeicutter上建立一個網站,我創建了一個名爲「bots」的新應用程序,並在模型中添加了一個名爲Trade的類,其中列出了兩個參數,「標題」和「單位」。遷移並運行服務器後,當我打開管理面板並單擊面板中的「+添加」按鈕創建交易(請參見圖片)。 Django的網頁返回此錯誤:django.db.utils.ProgrammingError:關係「bot_trade」不存在

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

產生額外的信息: 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

泊塢窗內運行我的Django的
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: 我刪除了遷移裏面的內容的文件夾了幾次,但是這是目前什麼是「0001.initial.py」內部運行「makemigrations」和「遷移」後:

# -*- 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'), 
     ), 
    ] 

當我運行 'showmigrations':

[email protected]:~/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遷移--fake機器人零'輸出:

[email protected]:~/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遷移機器人' 輸出:

[email protected]:~/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 

回答

0

你可能還沒有創建任何遷移您bot應用程序。您需要指定應用程序名稱,創建初始遷移:

./manage.py makemigrations bot 

然後運行遷移到運行遷移,並創建缺少的表:

./manage migrate 

當您運行showmigrations,你可以看到的Django認爲它已經爲您的bots應用程序應用了初始遷移。這可能是因爲你爲該應用運行了--fake

bots 
[X] 0001_initial 

你可以告訴Django,以紀念遷移爲未應用,然後重新運行遷移:

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

這應該工作,只要不從bots應用程序中創建表呢。如果只創建了一些表格,那麼修改數據庫將會更加棘手。

+0

嗨阿拉斯代爾,我已經這樣做了。我嘗試遷移 - 也是,謝謝你的回覆。 – Zalkota

+0

做假可能是問題所在 - 它將遷移標記爲未實際應用它。 – Alasdair

+0

我認爲如果您需要幫助,您需要提供更多信息。你的遷移是什麼樣的? 'manage.py showmigrations'輸出是什麼?貿易和單位的表格是否出現在數據庫中?當您嘗試查看「單元」的管理頁面時,是否也收到同樣的錯誤? – Alasdair

相關問題