簡體   English   中英

沒有要應用的遷移,但Django仍在嘗試創建新的內容類型

[英]No migrations to apply but Django is still trying to create a new content type

上周,我向服務器推送了一個新版本,其中包括針對新表的數據庫遷移。 這可以按預期完成,並且可以正常工作,但是現在在服務器運行的每個部署上,都在進行遷移,我看不到要應用任何遷移,但是在內容類型上卻遇到了唯一的關鍵錯誤。

Running migrations:
No migrations to apply.
...
File "/var/www/django/myproj/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.IntegrityError
(1062, "Duplicate entry 'djangocms_newsletter-signup' for key 'django_content_type_app_label_45f3b1d93ec8c61c_uniq'")

該表位於數據庫中, Signup模型的內容類型位於內容類型表中,遷移位於遷移表中...那么Django為什么還要嘗試創建新的內容類型?

遷移是我用於所有項目的簡單的部署后腳本的一部分。

#!/bin/bash

set -e

PROJ_PATH=/var/www/django/myproj

cd $PROJ_PATH

echo 'clear-out...'
find $PROJ_PATH -name "*.pyc" -exec rm -rf '{}' ';'
find $PROJ_PATH -name "*.pyo" -exec rm -rf '{}' ';'

echo 'set DJANGO_SETTINGS_MODULE'
export DJANGO_SETTINGS_MODULE="project.settings.local_override"

echo 'activating...'
source ../bin/activate

echo 'pip install...'
pip install -r requirements.txt --no-deps

echo 'migrate...'
python manage.py migrate --noinput

echo 'collectstatic...'
python manage.py collectstatic --noinput

echo 'restart apache...'
sudo service apache2 restart

deactivate

目前,我一直在清除現有內容類型中的應用名稱,以使部署成功,但現在我只想了解問題所在,以便將來解決。

完全追溯

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 165, in handle
emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/core/management/sql.py", line 268, in emit_post_migrate_signal
using=db)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 198, in send
response = receiver(signal=self, sender=sender, **named)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/contrib/contenttypes/management.py", line 56, in update_contenttypes
ContentType.objects.using(using).bulk_create(cts)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/models/query.py", line 409, in bulk_create
self._batched_insert(objs_without_pk, fields, batch_size)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/models/query.py", line 938, in _batched_insert
using=self.db)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/models/query.py", line 921, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 921, in execute_sql
cursor.execute(sql, params)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
return self.cursor.execute(query, args)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/var/www/django/myproj/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.
IntegrityError: (1062, "Duplicate entry 'djangocms_newsletter-signup' for key 'django_content_type_app_label_45f3b1d93ec8c61c_uniq'")
execute bash /var/www/django/myproj/myproj/shell_scripts/production_post_deployment.sh

再次遇到此問題后,我已使用服務器上的外殼調試了該問題,因為無法復制並發現該問題是由緩存的ContentType對象引起的。

讓我說明一下;

>>> content_types = dict((ct.model, ct) for ct in ContentType.objects.filter(app_label='consoles'))
>>> content_types
{}
>>> content_types = dict((ct.model, ct) for ct in ContentType.objects.filter(app_label='news'))
>>> content_types
{u'latestnews': <ContentType: latest news>}
>>> from django.core.cache import cache
>>> cache.clear()
>>> content_types = dict((ct.model, ct) for ct in ContentType.objects.filter(app_label='consoles'))
>>> content_types
{u'runner': <ContentType: runner>, u'importedclient': <ContentType: imported client>, u'placetype': <ContentType: place type>, u'invoice': <ContentType: invoice>, u'placetypetotal': <ContentType: place type total>, u'clientconsoles': <ContentType: client consoles>, u'emailtemplate': <ContentType: email template>, u'event': <ContentType: event>, u'occupation': <ContentType: occupation>, u'console': <ContentType: console>, u'meettheexperts': <ContentType: meet the experts>, u'auditlog': <ContentType: audit log entry>, u'proxyuser': <ContentType: user>, u'consoleuser': <ContentType: Console User>, u'paidby': <ContentType: paid by>, u'meettheexpertstransaction': <ContentType: Meet the experts Transaction>, u'clientuserconsole': <ContentType: client user console>, u'paymentmethod': <ContentType: payment method>, u'transaction': <ContentType: transaction>, u'country': <ContentType: Country>, u'client': <ContentType: client>, u'place': <ContentType: place>, u'consoletotal': <ContentType: console total>}

因此,對緩存所有內容類型的緩存管理器的修復已解決了該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM