1
votes

I am unable to run command runserver in my django app. It always show: You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them.

And when i am trying to migrate it will show this error: raise ValueError('The database backend does not accept 0 as a ' ValueError: The database backend does not accept 0 as a value for AutoField.

And this is the code in my models.py

class Category(models.Model):
    category_name = models.CharField(max_length=128, unique=True)
    category_alias = models.CharField(max_length=128, unique=True)
    category_desc = models.TextField()

    def __str__(self):
        return self.category_name

class Question(models.Model):
    #user = models.ForeignKey(User)
    category = models.ForeignKey(Category)
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

class Answer(models.Model):
    question = models.ForeignKey(Question)
    answer = models.TextField()

    def __str__(self):
        return self.answer

class UserProfile(models.Model):
    user = models.OneToOneField(User)

    state = models.CharField(max_length=200, blank=True)
    country = models.CharField(max_length=200, blank=True)
    zipcode = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return self.user.username

Can someone help me to come out of it.

Code in file 0003_question_user.py:

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

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('confession', '0002_userprofile'),
    ]

    operations = [
        migrations.AddField(
            model_name='question',
            name='user',
            field=models.ForeignKey(default=0,         to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
    ]
1
You don't seem to have posted the code for the failing migration. - Daniel Roseman
About which code you are asking? Now i have posted the error message too. - Anil Sharma
Well, it's failing on migration 0003_question_user in the confession app. What does that migration actually look like? - Daniel Roseman
when i am trying to run this command "python manage.py sqlmigrate confession 0003" It is throwing the same error. - Anil Sharma
I didn't ask that. I asked you what the code for that migration looks like. We can't help you without seeing it. - Daniel Roseman

1 Answers

2
votes
#user = models.ForeignKey(User)

is commented now. Did you try to execute migrate while it was uncommented ?

try python manage.py migrate --fake and then

python manage.py migrate