【问题标题】:manage.py migrate not being activated in prodcution...column X does not exist...why?manage.py migrate 未在生产中激活...列 X 不存在...为什么?
【发布时间】:2016-07-31 05:19:37
【问题描述】:

我收到此错误;

ProgrammingError at /
column main_category.parent_cat_id does not exist
LINE 1: ...n_category"."author_id", "main_category"."image", "main_cate. and I believe this means parent_cat_id isn't in the database. 

这是我不明白的,我将它设置为null=true 和blank=true....这在本地开发服务器中工作。该错误仅在生产中发生。(我使用的是弹性豆茎)

class Category(models.Model): 
    name = models.CharField(max_length=128, unique=True)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)

    parent_cat = models.ForeignKey('self', null=True, blank=True)
    hotCat = models.BooleanField(default=False)
    active = models.BooleanField(default=True)

    sponsored = models.ForeignKey(Sponsored, null=True, blank=True)


    objects = CategoryManager()

    def __unicode__(self): 
        return self.name

    def get_absolute_url(self):
        return "/category/%s/" %self.name

    def get_image_url(self):
        return "%s%s" %(settings.MEDIA_URL, self.image)

编辑:

这是我的 python 配置文件,你可以看到我已经迁移了

container_commands:
  01_migrate:
    command: "source /opt/python/run/venv/bin/activate && python myproject/manage.py migrate --noinput"
    leader_only: true

  02_uninstall_pil:
    command: "source /opt/python/run/venv/bin/activate && yes | pip uninstall Pillow"

  03_reinstall_pil:
    command: "source /opt/python/run/venv/bin/activate && yes | pip install Pillow --no-cache-dir"

  04_createsu:
    command: "source /opt/python/run/venv/bin/activate && python myproject/manage.py createsu"
    leader_only: true

  05_collectstatic:
    command: "source /opt/python/run/venv/bin/activate && python myproject/manage.py collectstatic --noinput"

  06_checkpermission:
    command: "source /opt/python/run/venv/bin/activate && python myproject/manage.py check_permissions"


option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "myproject.settings"
    "PYTHONPATH": "/opt/python/current/app/myproject:$PYTHONPATH"
  "aws:elasticbeanstalk:container:python":
    WSGIPath: myproject/myproject/wsgi.py

我的回溯

Traceback:
File "/opt/python/run/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/python/current/app/myproject/main/views.py" in index
  64.       request.session['categories'] = [ c.name for c in Category.objects.filter(author=request.user.id)] # add to the session 
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  162.         self._fetch_all()
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  965.             self._result_cache = list(self.iterator())
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  238.         results = compiler.execute_sql()
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  840.             cursor.execute(sql, params)
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  97.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/python/run/venv/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /
Exception Value: column main_category.parent_cat_id does not exist
LINE 1: ...n_category"."author_id", "main_category"."image", "main_cate...
                                                             ^

我的迁移文件

operations = [
    migrations.CreateModel(
        name='Category',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('name', models.CharField(unique=True, max_length=128)),
            ('description', models.TextField(verbose_name=b'\xec\xbb\xa4\xeb\xae\xa4\xeb\x8b\x88\xed\x8b\xb0 \xec\x84\xa4\xeb\xaa\x85')),
            ('image', models.ImageField(upload_to=b'images/', null=True, verbose_name=b'\xec\xbb\xa4\xeb\xae\xa4\xeb\x8b\x88\xed\x8b\xb0 \xeb\x8c\x80\xed\x91\x9c \xec\x9d\xb4\xeb\xaf\xb8\xec\xa7\x80', blank=True)),
            ('hotCat', models.BooleanField(default=False)),
            ('active', models.BooleanField(default=True)),
            ('author', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ('parent_cat', models.ForeignKey(blank=True, to='main.Category', null=True)),
        ],
    ),

【问题讨论】:

  • 您是否使用生产环境中模型的当前状态运行makemigrations 和migrate?确保您已将迁移文件提交到版本控制(我有时会忘记;))?
  • 我相信错误来自其他地方。你能发布完整的堆栈跟踪吗?
  • 您需要确保您的迁移文件夹包含最新的迁移,并且您在弹性 beanstalk 中的设置包括在 .ebextensions 文件夹的 python.config 中使用 manage.py migrate your_app
  • @schwobaseggl 感谢您的评论,我想我已经添加了它......你能检查一下我编辑的内容吗?我认为我所拥有的是迁移文件...不是 100% 确定
  • @AbhinavI 感谢您的评论我很漂亮这个错误意味着我在想我发布了回溯

标签: python django database postgresql amazon-web-services


【解决方案1】:

发生这种情况是因为在运行迁移脚本之前评估了一些使用此新字段的代码,具体如下:

File "/opt/python/current/app/myproject/main/views.py" in index
  64.       request.session['categories'] = [ c.name for c in Category.objects.filter(author=request.user.id)] # add to the session

解决方案是重写该代码使其不被评估,或者临时将其注释掉以便进行迁移。

这在本地没有发生,因为您可能在模型中定义了一个新字段,运行迁移脚本,然后编写了该视图代码。

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 2016-02-02
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2015-07-20
    • 2021-06-01
    • 1970-01-01
    相关资源
    最近更新 更多