django cms:插件模型与其数据库表不同步

时间:2015-05-06 15:30:19

标签: python django django-south django-cms

将django cms从版本2.4.3升级到3.0.11(目前为3.0.12)之后,我意识到某些型号已经不同步了#34;与其数据库表。例如:

class ProjectPagePluginModel(cmsPlugin):
    """
    CMS project plugin model.
    """
    project = models.ForeignKey(Project, on_delete=CASCADE)
    max_occurrences = models.PositiveIntegerField(default=0)

    def __unicode__(self):
        return self.get_plugin_name()




>>> ProjectPagePluginModel.objects.all()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/var/www/cms/venv2.7.5.up/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
ProgrammingError: relation "project_projectpagepluginmodel" does not exist
LINE 1: ...ct_projectpagepluginmodel"."max_occurrences" FROM "project_p...

另一方面,manage.py syncdb似乎没有帮助。有什么想法吗?

$ python manage.py syncdb
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Synced:
 > [etc.]

Not synced (use migrations):
 - [etc.]
 - project

更新:manage.py migrate也没有。

更新2:

我迁移的某些步骤出错了,我找到了丢失的信息。

首先,我进入postgresql shell找出这个插件信息的位置:

unicms2=> select * from information_schema.tables  where table_name like '%projectpage%';
 table_catalog | table_schema |            table_name            | table_type | self_referencing_column_name | reference_generation | user_defined_type_catalog | user_defined_type_schema | user_defined_type_name | is_insertable_into | is_typed | commit_action 
---------------+--------------+----------------------------------+------------+------------------------------+----------------------+---------------------------+--------------------------+------------------------+--------------------+----------+---------------
 unicms2       | public       | cmsplugin_projectpagepluginmodel | BASE TABLE |                              |                      |                           |                          |                        | YES                | NO       | 
(1 row)

unicms2=> select count(*) from cmsplugin_projectpagepluginmodel;
 count 
-------
 39180
(1 row)

然后,我进入django shell使用[south] [1]重命名表。

>>> from south.db import db
>>> db.rename_table('cmsplugin_projectpagepluginmodel', 'project_projectpagepluginmodel')

我认为这会有效,但后来我开始遇到运行时错误:

ProgrammingError: relation "cmsplugin_projectpagepluginmodel" does not exist
LINE 1: ...in_projectpagepluginmodel"."max_occurrences" FROM "cmsplugin...

1 个答案:

答案 0 :(得分:0)

我找到the inspiration来解决此错误。不知怎的,正如我所说,数据库与ORM-i不同步认真地认为它与django-cms 3的虚假升级有关 - 。

我必须完成以下所有申请:

$ python manage.py dumpdata >> fixture.json
CommandError: Unable to serialize database: relation "staff_staffpluginmodel" does not exist
LINE 1: ..., "staff_staffpluginmodel"."max_occurrences" FROM "staff_sta..."
$ mv staff/migrations/ ~/migrations-BAK/staff_migrations
$ python manage.py shell
>> from south.models import MigrationHistory
>> MigrationHistory.objects.filter(app_name='staff').delete()
>> quit()
$ python manage.py schemamigration --initial staff
$ vim staff/migrations/0001_initial.py   # comment out every command within forwards() except of those which create "staff_staffpluginmodel"
$ python manage.py migrate staff
$ python manage.py dumpdata >> fixture.json 

如果最后一个命令发出任何其他错误,只需重复该过程直到它没问题。

当然,这可以使用纯南迁移从django shell完成,而不是通过删除迁移文件和删除MigrationHistory。但是--auto参数使生活更轻松; - )

编辑 - &gt;使用南方数据迁移的更清洁的解决方案如下:

$ python manage.py datamigration staff migrate --freeze staff
-> edit the created staff/migrations/XXX.py migration, with the customised forwards and backwards methods
$ python manage.py migrate staff
-> enjoy

重复上面的代码片段,直到没有更多的ProgrammingError。