且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django表示“id可能不为NULL”但为什么呢?

更新时间:2023-11-05 17:03:46

我唯一可以想到的是,表模式已经从模型中脱离,因为有人从表的PK中删除了 AUTOINCREMENT 属性。


I'm going crazy today. I just tried to insert a new record and it threw back a "post_blogpost.id may not be NULL" error. Here's my model:

class BlogPost(models.Model):
    title   = models.CharField(max_length=100)
    slug    = models.SlugField(max_length=100)
    who     = models.ForeignKey(User, default=1)
    when    = models.DateTimeField()

    intro   = models.TextField(blank=True, null=True)
    content = models.TextField(blank=True, null=True)

    counter = models.PositiveIntegerField(default=0)

    published = models.BooleanField(default=False)
    css = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ('-when', 'id')

There are a number of functions beneath the model too but I won't include them in full here. Their names are: content_cache_key, clear_cache, __unicode__, reads, read, processed_content.

I'm adding through the admin... And I'm running out of hair.

The only thing I can think of is that the table schema has become desynchronized from the model in that someone removed the AUTOINCREMENT attribute from the PK of the table.