我正在使用 sqlite
数据库编写一个 django
网站,我的网站中有一个如下所示的模型:
class Foo(models.model):
name = models.Charfield(max_length=30)
数据库为该类创建一个唯一的 Id
列,假设我从类 Foo
创建一个对象,然后将其删除并创建另一个对象 g
,他们的Id
是相同的:
>> f = Foo.objects.create(name="fooname")
>> f.id
4
>> f.delete()
>> g = Foo.objects.create(name="fooname2")
>> g.id
4
但我不希望数据库将旧的已删除对象的 id
用于新创建的对象,我该怎么做?
最佳答案
这是一个已知错误,为 fixed在 Django 1.7 中,请参阅:
引自release notes :
AutoField columns in SQLite databases will now be created using the AUTOINCREMENT option, which guarantees monotonic increments. This will cause primary key numbering behavior to change on SQLite, becoming consistent with most other SQL databases. This will only apply to newly created tables. If you have a database created with an older version of Django, you will need to migrate it to take advantage of this feature.
关于python - 如何防止 django 数据库将已删除对象的 id 用于新对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23967183/