TypeError: __init__() missing 1 required positional argument: 'on_delete'

class HeroInfo(models.Model):
    # 英雄名
    hname = models.CharField(max_length=20)
    # 性别
    hgender = models.BooleanField(default=False)
    # 备注
    hcomment = models.CharField(max_length=200)
    # 外键
    hbook = models.ForeignKey('BookInfo')
    # 删除标记
    isDelete = models.BooleanField(default=False)

创建迁移文件时报错:

报错环境 python=3.6,django=2.2,PyMySQL=0.9.3

TypeError: __init__() missing 1 required positional argument: 'on_delete'

原因:

django2.0之后定义外键时需指定'on_delete'参数值。

详见django官方文档:https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey

 PS:

django2.1以后不再支持mysql5.5,python manage.py migrate时会报错:

 raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in
 your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)'
at line 1"))

使用mysql5.7即可。

posted @ 2019-05-13 20:50  第六夜想曲  阅读(210)  评论(0编辑  收藏  举报