简体   繁体   中英

Database backend error in django for foreign key

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,
        ),
    ]
#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM