-1

I know this question has been asked a couple of time but no previous answer was able to solve my problem.

I had a perfectly working model in Django that looked like this:

class Template(models.Model):
    mat = models.CharField(max_length=50, null=True)
    ...

I had many instances of this model and up to now I was very happy with it. I recently decided that instead of a Charfield, this model was better suited to work with a ForeignKey in this position instead.

To get into details, the attribute ''mat'' was previously only referring to the name of another object instance. I decided to change it to the full fledged instance with a ForeignKey instead, like it should have always been. Therefore, the model was modified as follows:

class Template(models.Model):
    mat = models.ForeignKey(Stock, on_delete=models.CASCADE, related_name='mat_stock', verbose_name="mat", null=True)
    ...

I followed this change with the regular */manage.py makemigrations, */manage.py migrate. While these two commands worked, I was unable to select any instance of Template in the shell without raising the following error:

OperationalError: (1054, "Unknown column 'myapp_template.mat_id' in 'field list'")

Similar situations I encountered were solved by manually adding a column in SQL with the following line:

ALTER TABLE database.myapp_template ADD mat INT;

Unfortunately this did not solve my problem.

I figured maybe the problem was that I already had instances of my object that had character values in the ''mat'' column. Django would expect integer values (specifically "id") after my migration, so I decided to create a completely new attribute for Template as follows:

class Template(models.Model):
    pos_mat = models.ForeignKey(Stock, on_delete=models.CASCADE, related_name='mat_stock', verbose_name="mat", null=True)
    ...

This, I thought, would delete (or disregard) the "mat" column and create new "pos_mat" columns with the desired properties without having to handle old character values that wouldn't fit with the requirements. From there on it should be like adding a completely new ForeignKey attribute.

After the required and successful */manage.py makemigrations, */manage.py migrate I am still unable to access an instance of my model in the shell. I still get the same unpleasing:

OperationalError: (1054, "Unknown column 'myapp_template.mat_id' in 'field list'")

Would anybody know how to convince Django to go along with my changes? I am skeptical that rolling back migrations to zero will help me (it did not solve my problems in the past) and I hope it will not come to the deletion of my data. It is acceptable for my model to have an empty field in this column since I added a null=True to my attribute.

Thank you very much for your help. Have a good day.

chenard612
  • 101
  • 2
  • 15

1 Answers1

0

I have solved my problem by rolling back to my last stable migration. From there I was able to migrate a model where 'mat' was absent and 'pos_mat' was the only attribute. This means my problem arose in the first migration from the old version of 'mat' to the new version of 'mat'. Basically keeping the same name but changing the attribute characteristics is a no go. I hope those with the same problem will be able to fix it with this.

chenard612
  • 101
  • 2
  • 15