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

django cms: plugin models out of sync with its database tables

将 django cms 从版本 2.4.3 升级到 3.0.11(当前为 3.0.12)后,我意识到某些模型是 "out of sync",其数据库 tables。例如:

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 也不迁移。

更新 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].

重命名 table
>>> 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...

我找到 来解决这个错误。不知何故,正如我所说,数据库与 ORM 不同步 - 我认真地认为它与 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 参数让生活更轻松;-)

编辑 -> 更简洁的解决方案,使用南方数据迁移,如下所示:

$ 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。