南迁移未更新psql表(django)

1 投票
1 回答
592 浏览
提问于 2025-04-30 07:39

总结:

我在跟一个南方的教程学习。

我修改了一个 models.py 文件,但当我运行“migrate”命令时,psql 表没有更新。我不知道为什么。

我编辑了 polls/models.py 文件。

我添加了一个类,叫做“Survey”。

我在“Question”类里添加了一行,来引用这个 Survey。

models.py

我添加了这个类 Survey

并在 Question 类里添加了这一行:

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 polls --auto

    你不能在没有迁移的应用上使用 --auto。请尝试 --initial。

    (app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py schemamigration polls --initial

    • 添加了模型 polls.Survey
    • 添加了模型 polls.Question
    • 添加了模型 polls.Choice
    • 创建了 0001_initial.py。你现在可以用以下命令应用这个迁移:./manage.py migrate polls

我仔细检查了一下,发现 Survey 还没有出现在 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 表没有更新。有什么建议吗?以下是命令的完整响应:

```
(app01)MoriartyMacBookAir13:getstartapp macuser$ sudo python manage.py migrate polls
运行 polls 的迁移:
- 没有什么需要迁移的。
- 正在加载 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 个回答

1

我觉得在你修改模型和添加新模型之前,得先运行 schemamigration --initial。先执行这个命令,然后再去修改模型和添加新的模型。接着运行 schemamigration --auto,最后再执行迁移命令。这样你的数据库就会相应地更新了。

步骤如下:

  • 先运行 python manage.py schemamigration polls --initial(在修改模型之前)
  • 修改 models.py 文件
  • 运行 python manage.py schemamigration polls --auto
  • 运行 python manage.py migrate polls

撰写回答