2017-03-16 102 views
0

我想在服務器上部署本地工作,但是當我運行python manage.py時,我遇到了psycopg2的問題。遷移時出錯 - psycopg2 Django

設置:

DATABASES = { 
    'default':{ 
     'ENGINE': 'django.db.backends.postgresql_psycopg2', 
     'NAME': '******', 
     'USER': '*****', 
     'PASSWORD': '**********', 
     'HOST': '*******', 
     'PORT': '5432' 
    } 
} 

模型有問題

class user(AbstractBaseUser, PermissionsMixin): 
    created = models.DateTimeField(auto_now_add=True) 
    first_name = models.CharField(max_length=50) 
    last_name = models.CharField(max_length=50) 
    email = models.EmailField(unique=True) 
    street = models.CharField(max_length=100, null=True, blank=True) 
    street_num = models.CharField(max_length=10, null=True, blank=True) 
    unit_num = models.CharField(max_length=10, null=True, blank=True) 
    city = models.CharField(max_length=50, null=True, blank=True) 
    postal_code = models.CharField(max_length=10, null=True, blank=True) 
    state = models.ForeignKey(state, null=True, blank=True) 
    country = models.ForeignKey(country, null=True, blank=True) 
    phone = models.CharField(max_length=50, null=True, blank=True) 

    is_staff = models.BooleanField(default=False) 
    is_active = models.BooleanField(default=True) 
    ..... 

class country(models.Model): 
    name = models.CharField(max_length=50) 
    code = models.CharField(max_length=10) 


class State(models.Model): 
    country = models.ForeignKey(country, on_delete=models.CASCADE) 
    name = models.CharField(max_length=50) 
    short_name = models.CharField(max_length=3) 
    group = models.CharField(max_length=2, null=True, blank=True, choices=GROUP_TYPE) 
    properties = models.ForeignKey(stateProperties, null=True, blank=True) 

遷移文件: ...

  ('brand', models.CharField(blank=True, max_length=30, null=True)), 
      ('exp_month', models.CharField(blank=True, max_length=2, null=True)), 
      ('exp_year', models.CharField(blank=True, max_length=2, null=True)), 
      ('country', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='common.country')), 
      ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')), 
      ('state', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='common.state')), 

.....

ERROR: 
Running migrations: 
    Applying contenttypes.0001_initial... OK 
    Applying contenttypes.0002_remove_content_type_name... OK 
    Applying auth.0001_initial... OK 
    Applying auth.0002_alter_permission_name_max_length... OK 
    Applying auth.0003_alter_user_email_max_length... OK 
    Applying auth.0004_alter_user_username_opts... OK 
    Applying auth.0005_alter_user_last_login_null... OK 
    Applying auth.0006_require_contenttypes_0002... OK 
    Applying auth.0007_alter_validators_add_error_messages... OK 
    Applying auth.0008_alter_user_username_max_length... OK 
    Applying profile.0001_initial...Traceback (most recent call last): 
    File "/home/instantuser/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute 
return self.cursor.execute(sql, params) 
psycopg2.ProgrammingError: relation "common_country" does not exist 


**django.db.utils.ProgrammingError: relation "common_country" does not exist** 

我在這裏錯過了什麼?

+0

你爲什麼認爲這是導致問題的用戶模型?你能否展示你的國家模式?此外,都是在同一個Django應用程序中的模型? –

+0

@MananMehta嘿,因爲這個:申請profile.0001_initial。一切工作正常使用sqlite開發服務器本地。這隻在使用psycopg2時纔會發生。但即時通訊也添加國家模型 –

+0

@AndreMendes你有沒有嘗試我在答案中提出的建議? –

回答

-1
  1. 評論國家模型和運行

python manage.py makemigrations app_name

  • 取消註釋國家模型和運行相同的上述命令(國家模式將正確重新制作)

  • 最後將更改應用到DB使用遷移:

  • python manage.py migrate

    0

    替換此:

    country = models.ForeignKey(country, null=True, blank=True) 
    

    有:

    country = models.ForeignKey(Country, null=True, blank=True) 
    

    而且應該這樣做,我相信。

    +0

    對不起,我把全部改爲小寫,試圖解決這個問題。我所有的模型都是小寫字母。在所有模型都是大寫之前。 –

    0

    我發現了這個問題。這是我的錯誤。我有一個.gitignore設置爲忽略遷移文件和文件夾。所以Country所在的遷移文件夾沒有被髮送到主分支。

    其固定。 thx尋求幫助。