0

I have removed one Foreign Key field from a model and added another, so from:

 class MyModel(models.Model):
     fk_a = models.ForeignKey(ModelA)

to

 class MyModel(models.Model):
     fk_b = models.ForeignKey(ModelB)

I autogenerated the migration, which includes this:

   migrations.AddField(
        model_name='mymodel',
        name='fk_b',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='myapp.modelb'),
    ),
   migrations.RemoveField(
        model_name='mymodel',
        name='fk_a',
    ),

If I open up MySQL and describe the table, before applying the migration, it looks like:

mysql> describe myapp_mymodel;
+---------------+---------+------+-----+---------+----------------+
| Field         | Type    | Null | Key | Default | Extra          |
+---------------+---------+------+-----+---------+----------------+
| id            | int(11) | NO   | PRI | NULL    | auto_increment |
| fk_a_id       | int(11) | NO   | MUL | NULL    |                |
+---------------+---------+------+-----+---------+----------------+

When I run the migration, I get the following error:

raise django.core.exceptions.FieldDoesNotExist: 
  MyModel has no field named u'fk_a'

What on earth is going on? I've reset, carefully checked the migration and database state, regenerated and rerun the migration and the same error occurs.

user31415629
  • 925
  • 6
  • 25
  • 1
    Using made-up variables like `MyModel` and `fk_a` could be hiding the problem. Please show the actual code and the full traceback. – Alasdair Jun 27 '17 at 12:18
  • try if this works. https://stackoverflow.com/questions/38311503/django-1-9-drop-foreign-key-in-migration – badiya Jun 27 '17 at 12:35

0 Answers0