2

I was trying to alter some column fields in my MySQL table by modifying the class "test" below and messed it up in a bad way, I also deleted the migration files.

I am new to models and migrations but I am trying to understand on how to modify a table or columns in a proper way.

1) I have seen people modify the test class directly, for example if I would like to change the decimal_places to 5 instead of 4 they just changed the number and ran makemigrations. This is how I did it, sometimes it worked and sometimes it didn't recognize any changes. And now the last time it messed up my unique_together() function.

After some research it seems that I should create a table with class test(models.Model) and then from there on If I would like to change something I should use the ALTER TABLE function?

2) Since I now have ruined a well functioning database I would like to delete my table and create a new models.py and do the setup all over again. Is this a "proper" way of doing it or is there something smoother than just DROP TABLE and deleting the models.py and migrations?

# Create your models here.
class test(models.Model):
 birth = models.DateTimeField(blank=True, null=True)
 published = models.CharField(primary_key=True, max_length=50)
 city = models.CharField(max_length=50)
 price = models.DecimalField(max_digits=10, decimal_places=4, null=True)
 something = models.CharField(max_length=5, blank=True, null=True)

class Meta:
    managed = True
    db_table = 'test'
    unique_together = (('published', 'city', 'price'),)

def __str__(self):
    return self.names

2 Answers 2

2

It seems like your issues were caused by you by adding a unique_together that includes a field that can have NULL values.

price = models.DecimalField(max_digits=10, decimal_places=4, null=True)

The field price is allowed to have NULL values in your database, as you defined null=True.

Your unique_together contains this nullable field however:

unique_together = (('published', 'city', 'price'),)

Most likely you have created multiple test objects in your database with no price specified. Then you created your unique_together restraint, which is violated because multiple entries have a NULL value in the price column.

The easiest option to fix it is probably this:

  • delete all your entries with test.objects.all().delete()
  • run the migrations (python manage.py migrate) which will now be possible as your constraint is no longer violated
  • then create the entries again

You will get a ValidationError when creating them as your constraint will be violated again. You need to figure out how to handle your conflicting entries, e.g. by changing one of the values (published, city or price) or just not storing the entries that cause the error.

Another option is to update your entries manually to remove any conflicts. Most likely it is enough to add a price to each entry to fix it.

You could also empty your test table through SQL. In this case you also need to get rid of the entries in the django_migrations table in your database, delete the migrations and run makemigrations and migrate again.

You should be able to fix your issues by deleting your test objects, running migrate and then creating them again though.

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

Comments

-1

1) Try migrate your_app zero if you still have your table and migrations 2) Delete migration files 3) Then rewrite your model if needed 4) makemigrations and migrate

Comments

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.