在生产中没有激活manage.py迁移...列X不存在......为什么?

时间:2021-10-31 18:00:28

I'm getting this error;

我收到了这个错误;

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. 

This is what I don't understand, I set it to null=true and blank=true....and this worked in local development server. The error is occuring only in production.(I'm using elastic beanstalk)

这是我不明白的,我将它设置为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)

Edit:

This is my python config file, as you can see I have migrate

这是我的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

My traceback

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...
                                                             ^

My migration file

我的迁移文件

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)),
        ],
    ),

1 个解决方案

#1


0  

This happens because some code that uses this new field is evaluated before the migration script is run, specifically here it is:

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

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

The solution is to either rewrite that code so it's not evaluated, or temporary comment it out so you can do the migration.

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

This didn't happen on local because you probably defined a new field in the model, run the migration script, then wrote that view code.

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

#1


0  

This happens because some code that uses this new field is evaluated before the migration script is run, specifically here it is:

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

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

The solution is to either rewrite that code so it's not evaluated, or temporary comment it out so you can do the migration.

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

This didn't happen on local because you probably defined a new field in the model, run the migration script, then wrote that view code.

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