1

With what command can I delete tables from a django db of a specific app/ only one table? I did find all sorts of things, but not how to get rid of a table.

Also while I am at this.

I create the models and hit syncdb and then I have the tables. If I want to update/add to those tables, do I run into problems?

3 Answers 3

4

Your best bet would be to get django-south installed in your machine.

if you are using pip, do pip install django-south

This allows you too migrate data forward and backwards.

This is very handy especially if you need to update tables, and new tables etc.

check it out.

adding south to apps are as easy as python manage.py schemamigration appname --initial

Make your changes in a model and run the following python manage.py schemamigration appname --auto

Once your data migration file has been created it'll tell you data is now ready to migrate.

Simply use python manage.py migrate appname

http://south.aeracode.org/docs/about.html

Hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

Cool. Thanks! Heard about that. Is pip build in or do I have to get that?
pip does not come standard, but easy install does. run the following in terminal, easy_install pip
if it is not too much to ask, could you maybe upvote as well.
0

If you are deleting a table, this is done in the model file of the specific app that you are trying to delete, there is no command for this, you just go into the file and delete it and then re-run syncdb, for your other question, the answer is the same.. every app folder should have a file called "models.py" and here is where the models which are, in this case, equivalent to tables are specified, along with their fields, you simply edit this to make any changes.

2 Comments

ApPeL's answer gave me a new perspective on why you might be doing this, I am guessing you are trying to sync an existing DB with a django install? Anyways, once you are working in that Django install you should always create new models / tables through Django as it will do this for you (automatically creating the DB tables), but I supposed it doesn't delete the table when you delete the model? If so, ApPel's solution would be correct as it just needs a script that goes into your database to sync it up
I can exactly tell you why I am doing this. I am working only in django. And I am trying to get a rather complex db going. So right now I am trying out different variations of that. To avoid having a messed up db I want to delete. Also. I wanted to know that, in case later when I am up and running I want to delete specific tables, I would like to know if that is possibile and how.
0
def reset():
    import install
    from django.db.models import get_models
    removed_tables = []
    exceptions = [] 
    for model in get_models():
        if model.__name__ not in ('User','Session','Group','Permission'):
            try:
                model.objects.all().delete() # So we can remove the table without complaints from the database server.
                CURSOR.execute('DROP TABLE %s' % model._meta.db_table)
                print "Dropped table %s from model %s" % (model._meta.db_table, model.__name__)
            except Exception as e:
                exceptions.append([model._meta.db_table, str(e)])
                continue
            removed_tables.append(model._meta.db_table)
    print "Removed %s tables" % len(removed_tables)
    syncdb()
    install.install() # A function that leads to the creation of my default data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.