简体   繁体   English

如何处理“Django - 数据库错误:没有这样的表”错误?

[英]How can I handle the "Django - Database error: no such table" error?

I'm pretty new to django and I'm stuck at the problem with models.我对 django 还很陌生,我一直在解决模型的问题。 Here is my users app's models file:这是我的用户应用程序的模型文件:

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse
from PIL import Image




class Schedule(models.Model):
    title = models.CharField(max_length=150)
    context = models.TextField()


class Input(models.Model):
    DAYS_OF_FITNESS = [
       ('month', '30'),
       ('month_2', '60'),
       ('month_3', '90'),
   ]
    weight = models.PositiveIntegerField(validators=[MinValueValidator(40)])
    age = models.PositiveIntegerField(validators=[MinValueValidator(12)])
    days = models.CharField(
    max_length=20, choices=DAYS_OF_FITNESS, default='30')
    athlete = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
    schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)

I'm trying to link each user with its input using OneToOneField and Schedule is linked as a foreign key to Input model我正在尝试使用 OneToOneField 将每个用户与其输入链接起来,并且 Schedule 被链接为 Input 模型的外键

But when I'm trying to migrate models, I'm getting this error:但是当我尝试迁移模型时,我收到此错误:

return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: users_input

I checked that Both models are in django settings, and I migrated them.我检查了两个模型都在 django 设置中,然后迁移了它们。 BTW, I'm using signals to save inputs automatically, (just for note if it helps)顺便说一句,我正在使用信号自动保存输入,(如果有帮助,请注意)

It's because that table doesn't exist in the database.这是因为该表在数据库中不存在。 To create tables you have to do the following.要创建表,您必须执行以下操作。 Execute the following commands in your terminal.在终端中执行以下命令。

python manage.py makemigrations

python manage.py migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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