resetting table id

200 views
Skip to first unread message

Gary Roach

unread,
Jul 3, 2016, 7:18:50 PM7/3/16
to django-users
Hi all


I'm still working on the official django tutorial
(https//docs.djangoproject.com/en/1.9/intro/tutorial/02/) on the section
that is adding the Question table.

I'm using Eclipse Neon with PyDev IDE
Django 1.9
Pyathon 3.5
SqLite DBMS
OS Debian Stretch Linux
KDE Desktop

While working with the database API I have managed to run the
auto-increment field up and can't find a way to reset it. Does anyone
know how to do this. There are a lot of suggestions out there but I have
yet to find one that works.

Gary R.

James Schneider

unread,
Jul 3, 2016, 7:54:24 PM7/3/16
to django...@googlegroups.com

That's a bit surprising. A quick Google search points at resetting the proper values in the sqlite_sequence table.

Other options:
You can drop the table and recreate it.
You can drop the database and recreate it.

Obviously those options can have detrimental effects on your data.

The better question is why would you?

The autoincrement fields for columns are meant to be autonomous and guaranteed unique. Toying with those values can and will lead to data inconsistencies. That's why there isn't a 'simple' way to reset that counter.

If you're only doing it to keep the numbers low while you're developing, it really isn't worth it. You'll quickly reach a point where it is not feasible to drop all of the data and start over, and you need to ensure your dev environment is as close to production as it can be.

-James


Gary Roach

unread,
Jul 3, 2016, 8:10:09 PM7/3/16
to django...@googlegroups.com
Thanks for the return.

Normally I would agree that messing around with the auto-increment is a bad idea. But I am working with the tutorial and found the mismatch on id's to be a bit of a distraction. Was the reset necessary, No. I did learn a lot in the process though. I finally found $echo "" > db.sqlite3 completely zeroed out the file and running the makemigrations an migrate restored everything original condition. All of this drove home the fact that db.sqlite3 is no more than a text file and can be thrown away and recreated any time I like. This might be handy in the future.

Thanks again.
Gary R.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXLFxY8Gm_Wo1%2BftkDmiW9LB65Rs67Z1Cau1wa%3DMyPhwA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Javier Guerra Giraldez

unread,
Jul 4, 2016, 3:54:40 AM7/4/16
to django...@googlegroups.com
On 4 July 2016 at 01:09, Gary Roach <gary71...@verizon.net> wrote:
> All of this drove home the fact that db.sqlite3 is no more than a text file
> and can be thrown away and recreated any time I like


wrong.

SQLite keeps the database on a single file, but its nowhere near a
"text file". you did truncate to a single byte ('\n', added by
default if you don't use the '-n' option on echo), and it had to be
thrown away and recreated. it would've been 'nicer' to just delete
it.

--
Javier
Reply all
Reply to author
Forward
0 new messages