没有这样的表:Schedule_playerweightlifting

时间:2018-07-06 16:35:35

标签: python django django-models

migrations.CreateModel(
    name='PlayerWeightLifting',
    fields=[
        ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
        ('time', models.IntegerField(default=-1)),
        ('medal', models.CharField(blank=True, choices=[('GOLD', 'GOLD'), ('SILVER', 'SILVER'), ('BRONZE', 'BRONZE'), ('FOURTH', 'FOURTH')], max_length=256)),
        ('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Schedule.Player')),
    ],
    options={
        'ordering': ['time'],
    },
),

这是来自0001_initial.py文件的我的模型 PlayerWeightLifting 。但是当我运行它时,它显示“没有这样的表:Schedule_playerweightlifting”,下面是我对应的模型:

class PlayerWeightLifting(models.Model):
    player = models.ForeignKey(Player, on_delete=models.CASCADE)
    weightlifting = models.ForeignKey(WeightLifting, on_delete=models.CASCADE, default='')
    time = models.IntegerField(default=-1)
    medal = models.CharField(max_length=256,blank=True, choices=MEDAL_CHOICES)

    def __str__(self):
        return str(self.weightlifting) + str(" ") + str(self.player)

    class Meta:
        ordering = ['time']

我还有另一种类似的模型类,它工作正常,我陷入了数据库问题。救救我!

这是来自admin.py的部分:

admin.site.register(PlayerWeightLifting)

运行 python manage.py makemigrations python manage.py migration 命令时,终端上没有显示任何类型的错误,但是当我打开admin时,仅显示,没有这样的表格,可能是什么原因?我什至尝试了 Python manage.py migration --run-syncdb ,我们将不胜感激!谢谢:)

1 个答案:

答案 0 :(得分:1)

Admin Screenshot

Aadil,我使用了您的代码,到最后似乎没问题。

我认为您是使用python manage.py inspectdb > models.py从数据库中创建这些模型的。 有时,数据类型有些混乱,在您的Date模型中我们没有得到我们想要的东西,应该有DateFieldDateTimeField,而default应该是 如下面的代码或NONE中所述。

顺便说一下,我的Models.py是:

from django.db import models

class Player(models.Model):
    pass
class WeightLifting(models.Model):
    pass

class PlayerWeightLifting(models.Model):
    player = models.ForeignKey(Player, on_delete=models.CASCADE)
    weightlifting = models.ForeignKey(WeightLifting, on_delete=models.CASCADE,default='')
    time = models.IntegerField(default='')
    medal = models.CharField(max_length=256,blank=True)

def __str__(self):
    return str(self.weightlifting) + str(" ") + str(self.player)

class Meta:
    ordering = ['time']

如果问题仍然存在,请尝试先创建模型,然后通过迁移创建数据库。看看是否可行。