【发布时间】:2021-11-20 17:29:26
【问题描述】:
我想做什么?
Django 不支持在 mysql 数据库中设置枚举数据类型。使用下面的代码,我尝试设置枚举数据类型。
错误详情
_mysql.connection.query(self, query) django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that 对应于您的 MariaDB 服务器版本,以便使用正确的语法 'NOT NULL,
created_atdatetime(6) NOT NULL,user_idbigint 附近 NOT NULL)' 在第 1 行")
我错过了什么吗?
具有所有选择的枚举类
class enumTokenTypes(models.TextChoices):
Registration = "Registration"
ForgotPassword = "Forgot Password"
模型中的用户令牌类
class tblusertokens(models.Model):
token_id = AutoField(primary_key=True)
token_type = EnumField(max_length=20, choices=enumTokenTypes.choices)
created_at = DateTimeField(auto_now_add=True, blank=True)
user = ForeignKey(tblusers, on_delete = models.CASCADE)
迁移中的用户令牌创建模型
class EnumField(CharField):
def db_type(self, connection):
return "enum"
migrations.CreateModel(
name='tblusertokens',
fields=[
('token_id', models.AutoField(primary_key=True, serialize=False)),
('token_type', clientauth.models.EnumField(choices=[('Registration', 'Registration'), ('Forgot Password', 'Forgotpassword')], max_length=20)),
('created_at', models.DateTimeField(auto_now_add=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='clientauth.tblusers')),
],
)
赏金问题
为函数设置 2 个参数以传递逗号分隔值和默认值。
【问题讨论】:
标签: python python-3.9 django-3.2