我正在使用Django 2.2,MySQL 8.0。定义模型时,我会创建一些类属性及其字段。保存操作后,PyCharm未报告错误。
from django.db import models
from django_mysql.models import JSONField
class Activity(models.Model):
sponsor = models.IntegerField
certificateOrNot = models.BooleanField
sponsorWay = models.SmallIntegerField
activityName = models.CharField(max_length=60)
activityPhoto = models.URLField(max_length=255)
prizeInfo = JsonField
activityDetails = models.IntegerField
startTime = models.DateField
endTime = models.DateField
conditionType = models.SmallIntegerField
conditionInfo = models.SmallIntegerField
sponsorPhoneNumber = models.CharField(max_length=20)
sponsorNickName = models.CharField(max_length=40)
sponsorWechatNumber = models.CharField(max_length=255)
participantAttention = models.BooleanField
shareJurisdiction = models.BooleanField
allowQuitOrNot = models.BooleanField
inviateFriends = models.BooleanField
inputCommandOrNot = models.BooleanField
participateWay = models.BooleanField
winnerList = models.BooleanField
participantDrawNumber = models.SmallIntegerField
def __str__(self):
return self.activityName
然后我同步数据库。
python3 manage.py makemigrations luckyDraw_1
python3 manage.py migrate
这时MySQL可以成功创建表,但是表中的字段不符合我的期望。
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| activityName | varchar(60) | NO | | NULL | |
| activityPhoto | varchar(255) | NO | | NULL | |
| sponsorPhoneNumber | varchar(20) | NO | | NULL | |
| sponsorNickName | varchar(40) | NO | | NULL | |
| sponsorWechatNumber | varchar(255) | NO | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
那么,怎么了?
答案 0 :(得分:0)
数据库表中缺少某些字段,因为最后缺少某些字段中的括号,例如,第一个字段应为sponsor = models.IntegerField()
而不是sponsor = models.IntegerField