South is not dropping a table for app actstream - django

Trying to drop tables with South for app django-activity-stream and then re-add it.
Process I used to drop table:
Deleted all previous migrations for the app to start fresh
Replaced models.py with an empty file
python manage.py schemamigration actstream --initial
python manage.py migrate actstream
But when I look at the database, the tables are still there.
Why aren't the tables dropping -- am I doing something wrong?

Process I would use to drop/re-add table:
Deleted all previous migrations for the app to start fresh
python manage.py schemamigration actstream --initial
python manage.py migrate actstream --fake
Replaced models.py with an empty file
python manage.py schemamigration actstream --auto
python manage.py migrate actstream
Maybe you can omit the 3 step

Related

Django Migrations - How can I migrate my own django models without any inbuild models like auth,sessions,sites,admin etc

I have a requirements so that its not possible to change given schema. I dont need any django inbuild models such as admin,auth,sessions,messages etc..
how can I migrate my models without the inbuild models.
I will be gratefull if you can help me.
Thank you.
You can migrate your own models by doing this:
python manage.py makemigrations appname
python manage.py sqlmigrate appname 0001 #It is autogenerated value after makemigrations
python manage.py migrate
This is the way you can do.

HOw to rebuild Django tables?

I was having problems with my dB, so I dropped the tables for one of my Django apps. I then ran makemigrations for the app and tried to migrate. It said everything was up to date. It didn't re-create the dropped tables.
How can I get Django to re-build the tables?
If you only drop your app tables and need to recreate the tables again. You also need to remove all the migrations entry from you app in the django_migrations table.
After you have done that you can run migrate again. Or if you want to reset your migrations files, remove all migrations from that app and run makemigrations and migrate
You have to delete all the files in app/migrations, except __init__.py
project
app1
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
app2
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
...
Then you can run python manage.py makemigrations and python manage.py migrate

Schema migration in django south

I am trying to use schema migration as i have my models in which i had two models previously and i have created three more models.
I have tried several commands like
python manage.py schemamigration appname --add modelname
python manage.py schemamigration appname
python manage.py schemamigration
python manage.py schemamigration appname --fake
but no luck
sometimes it gives me the error
You have not passed any of --initial, --auto, --empty, --add-model, --add-field or --add-index.
and sometimes this one
sage: manage.py schemamigration [options]
Creates a new template schema migration for the given app
manage.py: error: ambiguous option: --add (--add-field, --add-index, --add-model?)
i am new to django so want some help.
Finally got it
python manage.py schemamigration appname --auto

Django 1.6.1 and South

I am using south in my Django application and can't understand one thing. I have changed one field from IntegerField to CharField and made after it : manage.py app_name --auto --update. Then I looked at the list of migrations and saw that this migration has number 0014. But when I do manage.py app_name migrate south tries to migrate version 0004. How to specify 0014?
I assume you meant:
manage.py schemamigration app_name --auto --update
not
manage.py app_name --auto --update
You can specify a version with the migrate:
./manage.py migrate app_name 0014
or the full version name:
./manage.py migrate myapp 0014_change_charfield...

How to delete all data for one app in Django 1.4 now that reset is gone?

How do I delete all the data in the database for on Django app? In previous version manage.py reset APPNAME did the job, but that's been deprecated.
What are we supposed to do now if we want to delete all the data from an app using the command line?
reset and sqlreset were both just wrappers around other management commands. sqlreset in particular can be duplicate by simply running:
python manage.py sqlclear myapp
python manage.py sqlall myapp
reset only served to automatically run the result of sqlreset on the database. Personally, I think removing that is a fantastic idea. Still, if you want similar functionality, you can just pipe the output to your database's shell commands.
For PostgreSQL, for example:
python manage.py sqlclear myapp | psql mydatabase
python manage.py sqlall myapp | psql mydatabase
If you want single command that should work with most database types you can pipe the drop table statements, that sqlclear generates, to dbshell
python manage.py sqlclear myapp | python manage.py dbshell
from django.contrib.contenttypes.models import ContentType
for ct in ContentType.objects.all()
ct.model_class().objects.all().delete()
Now that Django integrates migrations by default, you first need to make migrations for your app are first unapplied then deleted.
Here is the command line that works at least with Django 1.8 (replacing by the application you want to delete all associated data and:
# First, update the DB so it thinks no migrations were applied to the app
python manage.py migrate --fake <app_name> zero
# Erase all migrations in the app folder
rm -r "<app_name>/migrations/*"
# Erase the application tables
python manage.py sqlclear <app_name> | python manage.py dbshell
# Recreate the app tables, that will be empty
python manage.py makemigrations <app_name>
python manage.py migrate <app_name>
DIY
If you want to do that from the command line, create the following custom command:
from django.core.management.base import AppCommand, CommandError
from django.utils.six.moves import input
from django.db import DEFAULT_DB_ALIAS, connections
class Command(AppCommand):
help = (
'Removes ALL DATA related to the given app from the database '
'by calling model.objects.all().delete() for all app models. '
'This also removes related data in other apps via cascade.'
)
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--noinput', '--no-input',
action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.',
)
parser.add_argument(
'--database', action='store', dest='database', default=DEFAULT_DB_ALIAS,
help='Nominates a database to reset. Defaults to the "default" database.',
)
def handle_app_config(self, app_config, **options):
app_label = app_config.label
database = options['database']
interactive = options['interactive']
db_name = connections[database].settings_dict['NAME']
confirm = (ask_confirmation(app_label, db_name)
if interactive else 'yes')
if confirm == 'yes':
for model in app_config.get_models():
model.objects.using(database).all().delete()
self.stdout.write('Reset done.\n')
else:
self.stdout.write("Reset cancelled.\n")
def ask_confirmation(app_label, db_name):
return input("""You have requested a reset of the application {app_label}.
This will IRREVERSIBLY DESTROY all data related to the app currently in
the {db_name} database, and return each table to empty state.
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel: """.format(**locals()))
Copy it to app/management/commands folder in any of your apps folders and run it with
./manage.py app_db_tables_reset any_installed_app_name
Ready-made package
The command is available in the django_commands package, you can install it with
pip install git+http://github.com/mrts/django-commands.git
and add it to INSTALLED_APPS to activate the command.
Tested with Django 1.9, it may or may not work with 1.8.