Skip to content
Advertisement

Django migrate ProgrammingError: syntax error at or near “”

I’m learning django (1.9.2) on a practice web site and a postgresql database with psycopg2. I defined a model with a certain attribute “preview” and later deleted the attribute entirely. Despite having removed it, django seems to be referencing that old definition perhaps from a cache or something. The makemigrations command seems to work fine, reflecting every change that I make to the model definition, but once I run the migrate command, then this error pops up.

(env) D:Web WorkspaceRat Race Websiteratrace>python manage.py migrate
Operations to perform:
  Apply all migrations: contenttypes, news, polls, auth, sessions, admin
Running migrations:
  Applying news.0003_auto_20160212_1209...Traceback (most recent call last):
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbbacken
dsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: syntax error at or near "9999999999999"
LINE 1: ...ER TABLE "news_news" ADD COLUMN "preview" varchar(9999999999...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangocoremana
gement__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangocoremana
gement__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangocoremana
gementbase.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangocoremana
gementbase.py", line 399, in execute
    output = self.handle(*args, **options)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangocoremana
gementcommandsmigrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbmigrat
ionsexecutor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini
tial)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbmigrat
ionsexecutor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbmigrat
ionsexecutor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbmigrat
ionsmigration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbmigrat
ionsoperationsfields.py", line 62, in database_forwards
    field,
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbbacken
dsbaseschema.py", line 396, in add_field
    self.execute(sql, params)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbbacken
dsbaseschema.py", line 110, in execute
    cursor.execute(sql, params)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbbacken
dsutils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbbacken
dsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbutils.
py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangoutilssix
.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:Web WorkspaceRat Race Websiteenvlibsite-packagesdjangodbbacken
dsutils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "9999999999999"
LINE 1: ...ER TABLE "news_news" ADD COLUMN "preview" varchar(9999999999...

The database tables are empty, so I suspect that deleting the database and making a new one might fix the issue for now, but I’d like a more concrete solution in case it happens again in the future where deleting the database is not an option.

Here’s what the model definition looks like.

from django.db import models

# Create your models here.
class News(models.Model):
    headline = models.CharField(max_length=100)
    content = models.CharField(max_length=100)
    pub_date = models.DateTimeField('date published')
    content_preview = models.CharField(max_length=100, blank=True)
    thumbnail = models.ImageField(upload_to='thumbnails/%Y/%m/%d/', blank=True)
    def __str__(self):
        return self.headline

There’s no “preview” attribute anymore, yet django doesn’t seem to get that.

Edit: Here’s the definition for the preview field

preview = CharField(max_length=9999999999999)

It produced an error saying it couldn’t be null, so I entered a one time default value "news preview". I assume the default value is for existing entries. At the time there were about 5 test entries. Just read on another post that the quotes I used when entering the one time default value was the likely cause of the problem. Something about psycopg2 not liking quotes on values?… It was a string value, so I thought I needed quotes.

Anyways, so now that I’ve cleared out the preview field, how do I make django forget it was ever there?

Advertisement

Answer

Going off the other comments and my own experience . . . when you get this error, go into your migrations folder and delete everything except the __init__.py. You can even delete the __pycache__ folder. Then run makemigrations and migrate again. I think that should fix the problem.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement