网站首页 文章专栏 Django不能创建数据表
Django不能创建数据表
创建于:2021-07-04 08:22:18 更新于:2025-05-23 19:27:42 羽瀚尘 542

Django不能创建数据表

解决了一个大问题

在运行

python3 manage.py makemigrations
 python3 manage.py migrate

之后,返回

Operations to perform:
  Apply all migrations: admin, auth, authtoken, contenttypes, sessions, sites, viewer
Running migrations:
  Applying viewer.0004_todo... OK

看着一切正常,但是数据库就是无法创建todo表。

后来发现在models.py中的Meta属性需要为True,完整的数据表为

class Todo(models.Model):
    gen_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)
    finish_time = models.DateTimeField(blank=True, null=True)
    properties =  models.TextField(blank=True, null=True)
    content = models.TextField(blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)
    father  = models.IntegerField(blank=True, null=True)
 
    class Meta:
	    ##  这里需要为True
        managed = True
        db_table = 'todo'

因为我其他的数据表都是自己导入的,所以填了False,今天这个照葫芦画瓢结果搞错了,浪费了不少时间。

发现的其他有用的命令

直接进入数据库

python3 manage.py dbshell

重新应用migrations

删除数据库中django_migrations中的对应记录可以重新应用migrate

删除 app 文件夹下的 migrations 文件夹中的 对应文件可以重新执行python3 manage.py makemigrations