AutoField in sqlite

It seems that AutoField in django behaves a bit weird when using sqlite.

When a new object is created, its integer primary key gets the largest value in the column at that time +1. Where in other dbs, that integer gets the largest value that ever existed in the column +1, causing the key to be lifetime unique.

How can this be an issue?

If you delete your last blog post and you don't take care of all the foreign keys pointing to it, comments for example, you will end up having all those comments pointing to your next blog post.

Why?

Django maps AutoField to this sql statement:

    "id" integer NOT NULL PRIMARY KEY

Sqlite seems to have added support recently for an AUTOINCREMENT keyword that behaves like the other dbs, so a better sql output would be:

    "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT

Ticket: #6947

Make a comment!

About Comments

Your email won't be published for any reason. It's only kept for archive reasons if i want to contact you. You can use markdown syntax.