4

I'm trying to use South to add a new URLField to a model, like:

class Document(models.Model):
    text = models.TextField()
    reference_page = models.URLField(blank=True, null=True)
    source_page = models.URLField(blank=True, null=True) # new field

However, when I run python manage.py schemamigration myapp --auto I get the error:

DatabaseError: column myapp_document.source_page does not exist
LINE 1: ...ext", "myapp_document"."reference_page", "myapp_doc...

I'm using PostgreSQL as my DB backend. I properly initialized my app for South and have already run migrations for it. I've made sure my Django and South installs are up to date.

Why would it be giving me this error now?

Edit: Oddly, if I manually created the column in the database, the schemamigration call succeeds, but of course the migrate call fails until I manually remove the column. This is bizarre.

Cerin
  • 60,957
  • 96
  • 316
  • 522

2 Answers2

5

I have been dealing with this issue for a couple months. The link shared by montiniz in a comment on the other answer had a 0-voted answer that resolved my issue! I'm posting it here to help someone else.. build the community of knowledge and all that.


Problem: schemamigration [app] --auto fails with a complaint about a missing column (often for a new field for which you are trying to migrate the schema ...)

In my case, it would only complain when modifying the Workgroup model (a models.py Model) but not any other models.

Solution: Look for another model that has a default keyword argument in a ForeignKey relation which references the model that is failing to migrate.

workgroup = models.ForeignKey('core.Workgroup', default=get_default_workgroup, null=True)

If you remove the default keyword argument, then run the management command, it should succeed. Then, just add the original default argument back.

Community
  • 1
  • 1
pztrick
  • 3,741
  • 30
  • 35
  • 1
    Your approach also worked for me. When reading more carefully through the error log from the failed South attempt, I saw a reference to another file that referred to that model. (In my case, a line of code that queries the model I was trying to update, in a dynamic form.) So I also commented out those dynamic fields, built and applied the migration, and then uncommented out the references to that newly modified model. – supermitch Feb 06 '14 at 03:09
1

Check your migration files closely, specially models. Looks like somebody has fiddled with it. If current migration number is 10, then models in migrations 9 and below must not have this new field listed in it. If its there, then remove it.

Arpit
  • 953
  • 7
  • 11