南迁移没有更新psql表(django)

时间:2014-10-29 02:50:54

标签: python django heroku django-south psql

要点: 我正在关注南方教程。 我更改了一个models.py文件,但是当我运行“migrate”时,它不会更新psql表。我不知道为什么不。

我编辑了民意调查/ models.py 我加了一节课,“调查” 我在“问题”中添加了一行来参考调查

models.py

我添加了课程Survey

我在课程问题中添加了这一行:

survey = models.ForeignKey(Survey)

所有其他已经存在(基本上按照django教程)

import datetime
from django.db import models
from django.utils import timezone

# Create your models here.

### Adding the Survey class just now, to practice using South
class Survey(models.Model):
  survey_name = models.CharField(max_length=190)
  pub_date = models.DateTimeField('date published')
  def __unicode__(self): #__str__ instead of __unicode__ if using Python 3, but unicode for python 2
    return self.survey_name
  def was_published_recently(self):
    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
  was_published_recently.admin_order_field = 'pub_date'
  was_published_recently.boolean = True
  was_published_recently.short_description = 'Published recently?' # for admin site


class Question(models.Model):
  survey = models.ForeignKey(Survey) ### this is a new line based on adding the Survey class above
  question_text = models.CharField(max_length=200)
  pub_date = models.DateTimeField('date published')
  def __unicode__(self): #__str__ instead of __unicode__ if using Python 3, but unicode for python 2
    return self.question_text
  def was_published_recently(self):
    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
  was_published_recently.admin_order_field = 'pub_date'
  was_published_recently.boolean = True
  was_published_recently.short_description = 'Published recently?' # for admin site


class Choice(models.Model):
  question = models.ForeignKey(Question)
  choice_text = models.CharField(max_length=200)
  votes = models.IntegerField(default=0)
  def __unicode__(self):
    return self.choice_text

终端

  1. 我做了schemamigration,它说它添加了模型polls.Survey

    (app01)MoriartyMacBookAir13:getstartapp macuser $ vim polls / models.py (app01)MoriartyMacBookAir13:getstartapp macuser $ python manage.py schemamigration民意调查--auto 您不能在没有迁移的应用上使用--auto。尝试 - 初始。 (app01)MoriartyMacBookAir13:getstartapp macuser $ sudo python manage.py schemamigration民意调查 - 初始

    • 添加了模型民意调查。调查
    • 添加了模型民意调查。问题
    • 添加了模型民意调查。选择 创建0001_initial.py。您现在可以使用以下命令应用此迁移:./ manage.py migrate polls
  2. 我仔细检查了psql表中不存在的调查:

    (app01)MoriartyMacBookAir13:getstartapp macuser$ heroku pg:psql
    ---> Connecting to HEROKU_POSTGRESQL_YELLOW_URL (DATABASE_URL)
    psql (9.3.5, server 9.3.3)
    SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
    Type "help" for help.
    
    app01::YELLOW=> \d
                                 List of relations
     Schema |                 Name                 |   Type   |     Owner      
    --------+--------------------------------------+----------+----------------
     public | account_emailaddress                 | table    | aqolirupsmmmqz
     public | account_emailaddress_id_seq          | sequence | aqolirupsmmmqz
     public | account_emailconfirmation            | table    | aqolirupsmmmqz
     public | account_emailconfirmation_id_seq     | sequence | aqolirupsmmmqz
     public | auth_group                           | table    | aqolirupsmmmqz
     public | auth_group_id_seq                    | sequence | aqolirupsmmmqz
     public | auth_group_permissions               | table    | aqolirupsmmmqz
     public | auth_group_permissions_id_seq        | sequence | aqolirupsmmmqz
     public | auth_permission                      | table    | aqolirupsmmmqz
     public | auth_permission_id_seq               | sequence | aqolirupsmmmqz
     public | auth_user                            | table    | aqolirupsmmmqz
     public | auth_user_groups                     | table    | aqolirupsmmmqz
     public | auth_user_groups_id_seq              | sequence | aqolirupsmmmqz
     public | auth_user_id_seq                     | sequence | aqolirupsmmmqz
     public | auth_user_user_permissions           | table    | aqolirupsmmmqz
     public | auth_user_user_permissions_id_seq    | sequence | aqolirupsmmmqz
     public | discover_author                      | table    | aqolirupsmmmqz
     public | discover_author_id_seq               | sequence | aqolirupsmmmqz
     public | discover_awedio                      | table    | aqolirupsmmmqz
     public | discover_awedio_id_seq               | sequence | aqolirupsmmmqz
     public | discover_user                        | table    | aqolirupsmmmqz
     public | discover_user_id_seq                 | sequence | aqolirupsmmmqz
     public | django_admin_log                     | table    | aqolirupsmmmqz
     public | django_admin_log_id_seq              | sequence | aqolirupsmmmqz
     public | django_content_type                  | table    | aqolirupsmmmqz
     public | django_content_type_id_seq           | sequence | aqolirupsmmmqz
     public | django_session                       | table    | aqolirupsmmmqz
     public | django_site                          | table    | aqolirupsmmmqz
     public | django_site_id_seq                   | sequence | aqolirupsmmmqz
     public | hello_greeting                       | table    | aqolirupsmmmqz
     public | hello_greeting_id_seq                | sequence | aqolirupsmmmqz
     public | polls_choice                         | table    | aqolirupsmmmqz
     public | polls_choice_id_seq                  | sequence | aqolirupsmmmqz
     public | polls_question                       | table    | aqolirupsmmmqz
     public | polls_question_id_seq                | sequence | aqolirupsmmmqz
     [other tables, but no more polls tables]
     public | south_migrationhistory               | table    | aqolirupsmmmqz
     public | south_migrationhistory_id_seq        | sequence | aqolirupsmmmqz
    (45 rows)
    
    app01::YELLOW=> \q
    
    1. 现在为了更新并创建polls_survey,我按照建议运行migrate命令...但是,psql表没有更新。有什么建议吗?以下是命令的完整响应:
    2. ```
          (app01)MoriartyMacBookAir13:getstartapp macuser $ sudo python manage.py migrate polls     运行民意调查的迁移:      - 没什么可迁移的。       - 加载民意调查的初始数据。     从0个夹具中安装0个对象     (app01)MoriartyMacBookAir13:getstartapp macuser $ sudo python manage.py syncdb     同步...     创建表格......     安装自定义SQL ...     安装索引......     从0个灯具安装了0个对象

      Synced:
       > django.contrib.admin
       > django.contrib.auth
       > django.contrib.contenttypes
       > django.contrib.sessions
       > django.contrib.messages
       > django.contrib.staticfiles
       > storages
       > evernote
       > discover
       > hello
       > south
       > django.contrib.sites
       > djrill
       > allauth.socialaccount.providers.linkedin
      
      Not synced (use migrations):
       - polls
       - allauth
       - allauth.account
       - allauth.socialaccount
       - allauth.socialaccount.providers.facebook
      (use ./manage.py migrate to migrate these)
      (app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py migrate polls
      Running migrations for polls:
      - Nothing to migrate.
       - Loading initial data for polls.
      Installed 0 object(s) from 0 fixture(s)
      (app01)MoriartyMacBookAir13:getstartapp macuser$ 
      
      (app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py convert_to_south polls
      This application is already managed by South.
      (app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py migrate polls
      Running migrations for polls:
      - Nothing to migrate.
       - Loading initial data for polls.
      Installed 0 object(s) from 0 fixture(s)
      (app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py schemamigration polls --auto
      Nothing seems to have changed.
      (app01)MoriartyMacBookAir13:getstartapp macuser$ 
      

      ```

      也就是说,polls_survey仍然不存在于psql表中,但syncdb和migrate没有效果......所以psql没有反映python描述的数据模型......

      有什么建议我在这里做错了吗?

      如果有用:settings.py文件中包含我的heroku psql登录详细信息,因此为什么我能够成功运行heroku pg:psql命令

1 个答案:

答案 0 :(得分:1)

我认为在更改模型和添加模型之前,您必须运行 schemamigration --initial 。运行 schemamigration --initial 后,更改模型并添加新模型。然后运行schemamigration --auto,然后运行命令。它应相应地更改您的数据库。

步骤

  • 运行python manage.py schemamigration民意调查 - 初始(在更改模型之前)
  • 更改models.py
  • 运行python manage.py schemamigration polls --auto
  • 运行python manage.py migrate polls