为什么Django将我所有模型的pk设置为'90'?

时间:2021-09-13 20:56:54

I'm running Django 1.5 with SQLite, and I have a model called Assignment. Whenever I create one, it gets created with the correct pk value. But, whenever I try to retrieve any Assignment from my database, it is always returned with a pk of 90. I've been fighting this for an hour, and I have to admit I'm completely confused.

我使用SQLite运行Django 1.5,我有一个名为Assignment的模型。每当我创建一个,它就会被创建为正确的pk值。但是,每当我试图从数据库中检索任何赋值时,它总是返回一个pk值为90。我已经打了一个小时了,我得承认我完全糊涂了。

Here's my code, if it's any use.

这是我的代码,如果有用的话。

class Assignment(models.Model):
    class Meta:
        app_label = 'holiday'
        unique_together = ('year', 'employee')

    year = models.PositiveIntegerField(db_index=True)
    employee = models.ForeignKey('bsc.Employee', db_index=True)
    days = models.PositiveIntegerField(null=True)

This, and a bunch of methods that calculate some values based on models related to this one. Nothing fancy.

还有一些方法,它们基于与此相关的模型计算一些值。没有什么幻想。

I've got to add that this model has had a somewhat rough past - with all my absent-mindedness, I had originally set year as the primary key, which quickly failed as soon as I added two Assignments to different employees. Maybe I should look at the DB schema and see if anything's wrong. Thankfully, the app hasn't made it to production yet, but hopefully this can be fixed without a full DB reset.

我必须补充的是,这个模式曾经有过一段不太顺利的经历——尽管我心不在焉,但我最初将年份作为主要的关键,当我给不同的员工增加了两个任务后,很快就失败了。也许我应该看看DB模式,看看是否有什么问题。值得庆幸的是,这款应用还没有投入生产,但希望这款应用能够在没有完全的DB重置的情况下得到修复。

1 个解决方案

#1


1  

If you had created 90 previous records then deleted the rows from your database; your database key index will still be set to what would have been the next primary key number in your database.

如果您已经创建了90个以前的记录,然后从数据库中删除行;您的数据库索引仍然会被设置为数据库中的下一个主键号。

The way to resolve this would be to as described in this other * post: SQLite Reset Primary Key Field

解决这一问题的方法是在另一个* post中描述:SQLite重置主键字段。

#1


1  

If you had created 90 previous records then deleted the rows from your database; your database key index will still be set to what would have been the next primary key number in your database.

如果您已经创建了90个以前的记录,然后从数据库中删除行;您的数据库索引仍然会被设置为数据库中的下一个主键号。

The way to resolve this would be to as described in this other * post: SQLite Reset Primary Key Field

解决这一问题的方法是在另一个* post中描述:SQLite重置主键字段。