south migration: "database backend does not accept 0 as a value for AutoField" (mysql)

user968808 picture user968808 · Dec 2, 2013 · Viewed 17.1k times · Source

I'm new to django and trying to have a Foreign key back to users for an assignee and reporter. But when i'm trying to apply the change with South i get the error

ValueError: The database backend does not accept 0 as a value for AutoField.

My Model Code:

class Ticket(models.Model):
    title = models.CharField(max_length=80)
    text = models.TextField(blank=True)
    prioritys = models.ForeignKey(Prioritys)
    ticket_created = models.DateTimeField(auto_now_add=True)
    ticket_updated = models.DateTimeField(auto_now=True)
    assignee = models.ForeignKey(User, null=True, related_name='assignee')
    reporter = models.ForeignKey(User, null=True, related_name='reporter')

    def escaped_text(self):
        return markdown.markdown(self.text)

    def __unicode__(self):
        return self.text

Answer

Craynic Cai picture Craynic Cai · Sep 25, 2017

If this happens when you run manage.py migrate (or manage.py syncdb in old versions), the reason maybe is that you have tried to add a foreign key to a model which uses AutoField as its primary key, and use 0 as the default value. Edit the migration file and remove the argument default=0 in AddField operations. It works for me in Django 1.10.