南迁移不更新psql表(Django)

2024-06-25 23:37:59 发布

您现在位置:Python中文网/ 问答频道 /正文

小结: 我在学南方教程。 我换了一个py模型文件,但当我运行“migrate”时,它不更新psql表。我不知道为什么不。在

我编辑了民意测验/模型.py 我加了一门课“调查” 我在“问题”中加了一行来指代调查

在模型.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轮询/模型.py (app01)MoriartyMacBookAir13:getstartapp macuser$python管理.py方案分配轮询--自动 不能在没有迁移的应用程序上使用--auto。试试--首字母。 (app01)MoriartyMacBookAir13:getstartapp macuser$sudo python管理.py方案分配轮询--初始

    • 增加型号投票。调查在
    • 增加型号投票。问题在
    • 增加型号投票。选择 创建0001_初始.py. 您现在可以使用应用此迁移:/管理.py迁移轮询

我再次检查了psql表中不存在调查:

^{pr2}$
  1. 现在为了更新它并创建polls_survey,我按照建议运行migrate命令。。。但是,psql表没有更新。有什么建议吗?以下是命令的完整响应:

```
(app01)MoriartyMacBookAir13:getstartapp macuser$sudo python管理.py迁移轮询 正在为轮询运行迁移: -没有要迁移的内容。 -正在加载轮询的初始数据。 已从0个设备安装0个对象 (app01)MoriartyMacBookAir13:getstartapp macuser$sudo python管理.py同步数据库 正在同步。。。 正在创建表。。。 正在安装自定义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\u调查在psql表中仍然不存在,但是syncdb和migrate没有任何影响。。。所以psql没有反映python描述的数据模型。。。在

有什么建议吗?在

如果有用的话:设置.py文件中有我的heroku psql登录详细信息,所以我能够成功地运行上面的heroku pg:psql命令


Tags: djangopyselffordatemodelsmigratepolls
1条回答
网友
1楼 · 发布于 2024-06-25 23:37:59

我认为在更改模型和添加模型之前,必须运行schemamigration initial。运行模式迁移初始之后,更改模型并添加新模型。然后运行schemamigration auto,然后运行migrate命令。它应该相应地改变你的数据库。在

台阶

  • 运行python管理.py方案分配初始轮询(在更改模型之前)
  • 改变模型.py在
  • 运行python管理.py计划自动投票
  • 运行python管理.py迁移轮询

相关问题 更多 >