【问题标题】:Django / MySQL / Python Programming Error ( 1064)Django / MySQL / Python 编程错误(1064)
【发布时间】:2021-03-12 01:44:07
【问题描述】:

当我运行 python manage.py migrate 时,我不断收到以下错误。

django.db.utils.ProgrammingError: (1064, "您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以了解在 'None 附近使用的正确语法) NOT NULL, user_id_idinteger NOT NULL)' 在第 1 行")

我正在运行 Python 3.8.6、mysqlclient 2.0.1 和 Django 3.1.3。

型号

from ..assets.provinces import PROVINCE_CHOICES
from ..assets.industries import INDUSTRY_CHOICES
from django.contrib.auth.models import User


class GrantProfile(models.Model):
    user_id = models.ForeignKey(User,
                             models.SET_NULL,
                             blank=True,
                             null=True)
    province = models.CharField(
        max_length=2,
        choices=PROVINCE_CHOICES,
        help_text="Province your organization is based out of"
    )
    company_size = models.IntegerField(help_text="Company size")

    industry = models.CharField(
        max_length=150,
        choices=INDUSTRY_CHOICES,
        help_text="Industry your organization is part of."
    )

    class Meta:
        db_table = "grantprofiles"

查看

# Grant Profile View
from rest_framework import viewsets
from ..serializers.grantprofile import GrantProfileSerializer
from ..models.grantprofile import GrantProfile


class GrantProfileView(viewsets.ModelViewSet):
    serializer_class = GrantProfileSerializer
    queryset = GrantProfile.objects.all()

管理员

from django.contrib import admin
from ..models.grantprofile import GrantProfile


# Grant Profile Admin

class GrantProfileAdmin(admin.ModelAdmin):
    list_display = ('user_id', 'province', 'company_size', 'industry')


admin.site.register(GrantProfile, GrantProfileAdmin)

序列化器


from rest_framework import serializers
from ..models.grantprofile import GrantProfile


class GrantProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = GrantProfile
        fields = ('user_id', 'province', 'company_size', 'industry')

我不确定我是否遗漏了什么?

【问题讨论】:

    标签: python mysql django django-rest-framework django-3.1


    【解决方案1】:

    似乎输入错误。在序列化程序中,您输入了 user_id。您的 GrantProfile 模型中没有这样的字段。它应该是用户。而且您在序列化程序中导入了两次 GrantProfile 模型。

    【讨论】:

    • 所以我更改了用户 -> user_id 但这不是问题。错误是说我正在为查询运行错误的语法。
    猜你喜欢
    • 2020-06-24
    • 1970-01-01
    • 2011-02-11
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    相关资源
    最近更新 更多