【问题标题】:Operational Error 1054 Unknown Column操作错误 1054 未知列
【发布时间】:2018-08-16 20:16:22
【问题描述】:
class ProjectTimeAllocation(models.Model):
    id = models.IntegerField(primary_key=True)
    project_id = models.ForeignKey(Projects, models.DO_NOTHING, related_name='pj_id', blank=True, null=True)
    project_manager_id = models.ForeignKey(ProjectManagers, models.DO_NOTHING, blank=True, null=True)
    hours = models.IntegerField(blank=True, null=True)
    week = models.DateTimeField(blank=True, null=True)
    date_created = models.DateTimeField(auto_now=True, null=True)
    last_updated = models.DateTimeField(blank=True, null=True)


    class Meta:
        managed = False
        db_table = 'project_time_allocation'

views.py

def timeallocation(request, project_manager_id):
    #time_list = ProjectTimeAllocation.objects.order_by('-id')
    if request.method == "POST":
        form = ProjectTimeAllocationForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            #post.project_manager_id = project_manager_id
            post.save()
            messages.success(request, "Updated Time!")
            return HttpResponseRedirect('/dashboard/')
    else:
        form = ProjectTimeAllocationForm()
    return render(request, 'dashboard/timeallocation.html', {'form':form})

forms.py

class ProjectTimeAllocationForm(forms.ModelForm):
    week = forms.DateField(widget=forms.SelectDateWidget())



    class Meta:
        model = ProjectTimeAllocation       
        fields = (
            'hours',
            'week',
            'project_manager_id',
            'project_id',
            )
        widgets = {

        }
        labels = {

        }

我收到一个操作错误(1054,“字段列表中的未知列 'project_id_id'。我不明白额外的 _id 来自哪里?我已尝试清除迁移并进行新的迁移。

【问题讨论】:

    标签: operationalerror


    【解决方案1】:

    django 似乎在 ForeignKeys 上附加了 _id,因此我不得不从模型和表单中的列名中删除 _id,然后 django 在生成 sql insert 语句时将其重新附加,现在问题已解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 2012-03-09
      • 2017-03-17
      • 1970-01-01
      • 2011-11-12
      • 2020-09-25
      • 2015-08-19
      • 2021-08-05
      相关资源
      最近更新 更多