0

I'm trying to make migrations on my app, and I'be followed the advice supplied here: django 1.7 migrate gets error "table already exists"

Specifically:

Or if you want to avoid some actions in your migration, you can edit the migration file under the app/migrations directory and comment the operations you don't want to do in the migrate execution.

So now my migration file looks like so:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('gantt_charts', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Dependency',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        # migrations.CreateModel(
        #     name='Task',
        #     fields=[
        #         ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
        #         ('title', models.CharField(max_length=100)),
        #         ('description', models.TextField(blank=True)),
        #         ('completed', models.BooleanField(default=False)),
        #         ('project', models.ForeignKey(related_name='tasks', to='gantt_charts.Project')),
        #         ('subtasks', models.ManyToManyField(blank=True, through='gantt_charts.Dependency', related_name='supertasks', to='gantt_charts.Task', null=True)),
        #     ],
        #     options={
        #     },
        #     bases=(models.Model,),
        # ),
        migrations.AddField(
            model_name='dependency',
            name='subtask',
            field=models.ForeignKey(related_name='dependencies_as_subtask', to='gantt_charts.Task'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='dependency',
            name='supertask',
            field=models.ForeignKey(related_name='dependencies_as_supertask', to='gantt_charts.Task'),
            preserve_default=True,
        ),
        migrations.AlterUniqueTogether(
            name='dependency',
            unique_together=set([('supertask', 'subtask')]),
        ),
    ]

The issue is now the migration tools is complaining:

  • LookupError: App 'gantt_charts' doesn't have a 'task' model.
  • ValueError: Lookup failed for model referenced by field gantt_charts.Dependency.supertask: gantt_charts.Task

My suspicion is that some how I've broken my DB/Django by having the table exist, and Django not know about it. For some reason it's not checking the DB or my models.py file for the existance of the task model.

How can I get the migration to work?

FWIW, and because it might be relevant I'm on SQLite and have a self-referential field (subtasks). The docs say that SQLite can be buggy and this might be one of those instances.

Community
  • 1
  • 1
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
  • I've just tried the 'fake' migration and it now can't find my dependency table: `no such table: gantt_charts_dependency`. So I'll [rollback](http://stackoverflow.com/a/5814593/1075247) and wait for advice – AncientSwordRage Jan 26 '15 at 12:17
  • Have only worked with south, so no real 1.7 migration experience, BUT: how about you split your migration into multiple migration files. Have the first one (which you'll fake) contain everything there is to know about the table that already exists, and then keep in the next migrations stuff that depend on the previous migration.... sort of like solving circular imports - move code around in multiple files. – vlad-ardelean Jan 26 '15 at 12:26
  • @vlad-ardelean the thought crossed my mind but I was hoping I wouldn't need to hand-carve a migration. The problem is that my Task model relies on the Dependency model via a 'through' kwarg. Is that not going to be a problem? – AncientSwordRage Jan 26 '15 at 13:56
  • I ended up nuking the DB and starting again – AncientSwordRage Jan 26 '15 at 16:37

0 Answers0