0

After upgrading django cms from version 2.4.3 to 3.0.11 (currently it's 3.0.12) I have realised that some models are "out of sync" with its database tables. For example:

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

On the other hand, manage.py syncdb doesn't seem to help. Any idea?

$ 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

Update: neither does manage.py migrate.

Update 2:

Some step of my migration went wrong, I have found the information I had lost.

First, I went into postgresql shell to find out where this plugin information was:

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)

Then, I went into the django shell to rename the table using [south][1].

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

I thought this would work, but then I started getting run time errors:

ProgrammingError: relation "cmsplugin_projectpagepluginmodel" does not exist
LINE 1: ...in_projectpagepluginmodel"."max_occurrences" FROM "cmsplugin...
sogeking
  • 1,216
  • 2
  • 14
  • 45

1 Answers1

0

I found the inspiration to solve this error. Somehow, as I said, the database got out of sync with the ORM -I seriously think it had something to do with a bogus upgrade to django-cms 3-.

I had to go through all my applications as follows:

$ 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 

If the last command gives any other error, just repeat the process until it's ok.

Of course, this could be done from the django shell using plain south migrations instead of going through dropping the migration files and deleting the MigrationHistory. But the --auto parameter makes life easier ;-)

EDIT -> a cleaner solution, using south data migrations, would be as follows:

$ 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

Repeat the above code snippet until there are not more ProgrammingError.

Community
  • 1
  • 1
sogeking
  • 1,216
  • 2
  • 14
  • 45