MySQL在“字段列表”中给出“未知列”user.id“错误使用Django的自动ID [英] MySQL gives an "Unknown column 'user.id' in 'field list'" error using Django's automatic id

查看:333
本文介绍了MySQL在“字段列表”中给出“未知列”user.id“错误使用Django的自动ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的用户模型设置没有主键,所以将使用自动 id 。但是,当我通过外键引用时,尝试使用Django的 _set 符号进行访问:

I have my User model set up with no primary key so that the automatic id will be used instead. However, when I try to access it using Django's "_set" notation when it is referenced through a foreign key:

def postDetails(request, pk)
    post = Post.objects.get(pk=pk)
    if post.user_set.all(): # Errors on this line
        [...]

我从MySQL收到错误:

I get an error from MySQL:


/ webApp / postDetail / 42 /(1054,
操作错误'字段列表'中的未知列'user.id')

OperationalError at /webApp/postDetail/42/ (1054, "Unknown column 'user.id' in 'field list'")

我做错了什么?我应该访问不同吗?自动ID有限制吗?

What am I doing wrong? Should I be accessing it differently? Are there limitations to the automatic id?

参考模型:

class Post(models.Model):
    name = models.CharField(max_length=128)
    blog = models.ForeignKey('Blog')
    active = models.BooleanField(blank=True)
    created_date = models.DateTimeField()
    class Meta:
        managed = False
        db_table = 'post'
    def __unicode__(self):
        return self.name

class User(models.Model):
    level = models.ForeignKey(Level)
    post = models.ForeignKey(Post)
    name = models.CharField(max_length=64)
    note = models.CharField(max_length=4096)
    active = models.BooleanField(blank=True, default=True)
    class Meta:
        managed = False
        db_table = 'user'

我以为包含的其他东西:I在运行此程序之前运行 syncdb

Something else that I thought to include: I did run syncdb before running this.

推荐答案

不是100%确定,但我认为即使Django会将id字段添加到模型类中,该字段将不会传播到具有syncdb的数据库。

Not 100% sure, but I think even though Django will add the id field to the model class, that field will not propagate to the DB with syncdb.

尝试它的一种方法是重命名现有的用户表,运行syncdb并查看是否创建了用户表。如果没有(可能是由于托管标志),请再次尝试使用managed = True。如果在这种情况下出现id字段,那么我的猜测是你必须手动添加到User表中,与自动创建的参数相同。

One way to try it would be to rename the existing User table, run syncdb and see if the User table is created. If not (which is likely because of the managed flag) try again with managed=True. If the id field appears in this case then my guess is you'll have to add it manually to the User table with the same parameters as the automatically created one.

这篇关于MySQL在“字段列表”中给出“未知列”user.id“错误使用Django的自动ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆