3

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 Workspace\Rat Race Website\ratrace>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 Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.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 Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini
tial)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\operations\fields.py", line 62, in database_forwards
    field,
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\base\schema.py", line 396, in add_field
    self.execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\base\schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\utils.
py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\utils\six
.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.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?

9
  • It seems it is failing when trying to add the column, which could be a previous migration that you didn't run. What was the definition for the preview field? Commented Feb 12, 2016 at 14:57
  • Just added that to the question. Does that help? Commented Feb 12, 2016 at 15:28
  • You need to modify your migrations (either edit them or delete/recreate). However i think that the field length may be the cause. I can check that. Commented Feb 12, 2016 at 15:34
  • 1
    Try changing the 9999999999999 to a 50 or something in your migration and try running it again. Commented Feb 12, 2016 at 15:37
  • i added this line: preview = models.CharField(max_length=50, blank=True) back to the model definition. still getting the same error Commented Feb 12, 2016 at 16:09

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

This can also happen when making a CharField nullable if you do not specify max_length.

So the solution is to specify max_length in your model, then remove and remake the problematic migration..

This was described as a bug here.

Comments

0

This is old, but I will answer anyway.

The problem is that you made a "makemigrations" with a mistake in your model and that was catalogued, I will assume you corrected the mistake, that catalogue is not deleted, instead it was aggregated when you tried Migration.

What you need to do is delete everything in your migration folder except init, that should clear that up, if it doesn't then you still have a mistake in your Model

1 Comment

This looks almost the same as the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.