mysql user_objects,MySQL在“字段列表”中给出“未知列”user.id“错误使用Django的自动ID...

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

[...]

I get an error from MySQL:

OperationalError at /webApp/postDetail/42/ (1054,

"Unknown column 'user.id' in 'field list'")

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

Model for reference:

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'

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

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值