Opened 8 years ago

Closed 8 years ago

#25832 closed Bug (invalid)

Migrating a django-cms.PlaceholderField fails when using Postgres

Reported by: Jon Ribbens Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It seems to me that 'makemigrations' creating broken migrations is a bug. The django-cms people say it is a django bug not a django-cms bug, hence my ticket here (see https://github.com/divio/django-cms/issues/4789 ).

$ pip install djangocms-installer psycopg2
$ djangocms -sqp . -d postgresql://test:test@localhost/test my_demo
$ python manage.py startapp testapp

Add testapp as penultimate entry in my_demo/settings.py:INSTALLED_APPS.
Edit testapp/models.py:

from django.db import models
class TestModel(models.Model):
    description = models.TextField("Description")
$ python manage.py makemigrations testapp
$ python manage.py migrate

Edit testapp/models.py:

from django.db import models
from cms.models.fields import PlaceholderField
class TestModel(models.Model):
    description = PlaceholderField("Description")
$ python manage.py makemigrations testapp
$ python manage.py migrate
...
Running migrations:
  Rendering model states... DONE
  Applying testapp.0002_auto_20151129_1448...Traceback (most recent call last):
  File "test/env/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "description_id" cannot be cast automatically to type integer
HINT:  You might need to specify "USING description_id::integer".

This only happens with Postgres (or rather, it doesn't happen with sqlite, i.e. if you remove the -d from the djangocms invocation above). It only happens if the column is being changed in one migration, if it's split into two migrations the first of which deletes the column and the second of which creates it, there is no issue.

I was originally actually getting "ValueError: Related model 'cms.Placeholder' cannot be resolved", but I cannot reproduce that so easily since I already fixed it in my project - see mailing list thread https://groups.google.com/forum/#!topic/django-cms/tIT3g0gCpwc.

Change History (1)

comment:1 by Simon Charette, 8 years ago

Resolution: invalid
Status: newclosed

Hi @jribbens,

The ProgrammingError you're getting should be fixed in Django 1.9 (#25002) as long as your description text field contain only data that can be casted to an integer.

If it's not the case, which is very likely given it's a free text field, you either have to drop the original column and create a new one just like you described or do the following:

  1. Create a migration that adds the PlaceholderField to your model (which is just a disguised nullable foreign key to cms.Placeholder).
  2. Create a data migration that populates the newly added PlaceholderField based on the original description text field.
  3. Create a migration that removes the original description text field and rename the newly added PlaceholderField field if required.

Hope it helps.

Note: See TracTickets for help on using tickets.
Back to Top