delete tables

906 views
Skip to first unread message

Mirto Silvio Busico

unread,
Oct 17, 2008, 8:30:18 AM10/17/08
to Django users
Hi all,

what is the django way o deleting all table content and resetting the
primary key counter?

Thanks
Mirto

--

_________________________________________________________________________
Busico Mirto Silvio
Consulente ICT
cell. 333 4562651
email m.bu...@ieee.org

Erik Allik

unread,
Oct 17, 2008, 8:36:58 AM10/17/08
to django...@googlegroups.com
$ manage.py dbshell
> DROP TABLE table_name;

Django does not currently have a database schema management tool (with
the exception of syncdb and sql* commands).

Erik

Mirto Silvio Busico

unread,
Oct 17, 2008, 9:41:46 AM10/17/08
to django...@googlegroups.com
Sorry I was not clear.

I have some utilities in view.py which have to reload tables content
(migration from the legacy database; clearing a table and reloading with
base content; ...)

I know from the shell it is enough an "python manage py flush", but I
have two problems:

* the action have to be triggered by a user action on a form
* the id have to be resetted because for me the id=0 and id=1 rows
have a fixed special meaning

I have not found anything about deleting tables in
http://docs.djangoproject.com/en/dev/topics/db/queries/ (it seems that
"database api" has gone in 1.0)
The only thing that seems I can use is "Executing custom SQL" in
http://docs.djangoproject.com/en/dev/topics/db/models/

There is a better/recommended method?

Thanks
Mirto


Erik Allik ha scritto:

felix

unread,
Oct 17, 2008, 10:10:59 AM10/17/08
to django...@googlegroups.com

here's what I'm using:

    from django.core import management
    from django.db import connection
    cursor = connection.cursor()
    # don't delete these tables
    # note that I'm also keeping auth_user
    tables = ['comments','objekt_objekt','tag','tags','user_objekt_tag','auth_user','auth_session']

    current_tables = connection.introspection.table_names()
   
    for table in current_tables:
        if table not in tables:
            try:
                cursor.execute("drop table %s" % table)
            except Exception,e:
                raise e
                #pass

    management.call_command('syncdb')

Steve Holden

unread,
Oct 17, 2008, 10:29:35 AM10/17/08
to django...@googlegroups.com
Mirto Silvio Busico wrote:
> Sorry I was not clear.
>
> I have some utilities in view.py which have to reload tables content
> (migration from the legacy database; clearing a table and reloading with
> base content; ...)
>
> I know from the shell it is enough an "python manage py flush", but I
> have two problems:
>
> * the action have to be triggered by a user action on a form
> * the id have to be resetted because for me the id=0 and id=1 rows
> have a fixed special meaning
>
> I have not found anything about deleting tables in
> http://docs.djangoproject.com/en/dev/topics/db/queries/ (it seems that
> "database api" has gone in 1.0)
> The only thing that seems I can use is "Executing custom SQL" in
> http://docs.djangoproject.com/en/dev/topics/db/models/
>
> There is a better/recommended method?
>
> Thanks
> Mirto
>
>
> Erik Allik ha scritto:
>> $ manage.py dbshell
>> > DROP TABLE table_name;
>>
>> Django does not currently have a database schema management tool (with
>> the exception of syncdb and sql* commands).
>>
If 0 and 1 have special significance as primary key values then it's
very fragile to rely on the database to insert them for you. Instead
you'd be better off writing them specifically.

It's not possible to reset the automatically generated IDs in a
platform-independent way because of the huge differences between (say)
PostgreSQL serials vs. Oracle sequences (which as far as I can tell
actually require a stored procedure to increment the sequence and insert
the value into a table, though I am no Oracle expert).

If you can suggest a suitable API then it might be considered for
inclusion into Django in some future version. The API reference is still
available, by the way, at

http://docs.djangoproject.com/en/dev/topics/db/queries/

In the meantime you are left with raw SQL as the only suitable way to do
what you want, and the API documentation does give you some guidance as
to how to implement raw SQL. As for the exact details of the counter
reset, you'd need to tell us which database engine you are running against.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Mirto Silvio Busico

unread,
Oct 17, 2008, 10:34:01 AM10/17/08
to django...@googlegroups.com
Wanderful!
This was exactly what I needed!

There is any documentation about "call_command" and the other commnds you used?


Thanks alot.
    Mirto

felix ha scritto:

felix

unread,
Oct 17, 2008, 1:11:40 PM10/17/08
to django...@googlegroups.com

I just pulled it directly from django's source code.

I would only recommend doing syncdb in a development environment.  I just use it as part of an import / convert from an old website's schema.

probably for what you are doing (a user doing something on a form) this is too dangerous to do.

syncdb generates sql
checks for an admin user
and installs any initial_data fixtures

by looking at how it does that you should be to isolate just the commands you want for just the application and db tables that you want.  look at manage.py and keep following the code.

if you know which db you will be running on then maybe its better just to delete the models and then reset the auto increment sequence using raw sql as steve holden suggested.

maybe better is to delete all the models except for the 0 and 1
so they will still be there.

-f;lix
Reply all
Reply to author
Forward
0 new messages