I would like to completely empty the entire database, restoring it to the way it was when I just created it, using Django's manage.py. Possible?
What you can do to flush the DB and not have any migrate(south) problem afterwards is:
first, reset the data from the DB:
python manage.py flush
second, fake the migrations that are already applied:
python manage.py migrate --fake
third, if you have some fixture to load:
python manage.py loaddata my_sweet_json_file
Yes, you can use flush.
That will reset and restore everything in your entire database, regardless of which app or project the models are in. If you have a few databases, you can specify a single one in particular by using the --database switch
Examples:
python manage.py flush
python manage.py flush --database mydatabase
Related
I have the initial data from my old database which takes around 6GB. I could "dumpdata" my old database without any problem. But when I attempted to restore them to the new database, I got the MemoryError:
python manage.py loaddata fixtures/initial_data.json
MemoryError: Problem installing fixture 'fixtures/initial_data.json':
Is there any way to make loaddata work with chunks or is it possible to load that big file?
I've wrote this script, which is a fork of django's dumpdata, but dumps data in chunks to avoid MemoryError. And then load these chunks one by one.
Script is available at https://github.com/fastinetserver/django-dumpdata-chunks
Example usage:
1) Dump data into many files:
mkdir some-folder
./manage.py dumpdata_chunks your-app-name
--output-folder=./some-folder --max-records-per-chunk=100000
2) Load data from the folder:
find ./some-folder | egrep -o "([0-9]+_[0-9]+)" | xargs ./manage.py loaddata
PS. I used it to move data from Postgresql to MySQL.
For large database use backup tools for dumping database data instead "django dumpdata".
To load database data use restore tools instead "django loaddata".
I'm looking for the updated Django 1.5 command that can do the following action.
python manage.py reset <app>
What I want to do basically is DROP tables and UPDATE the database structure inside with a manage.py command.
The thing is that reset command is no longer working and
manage.py flush
or
manage.py sqlclear <app>
are just dropping the database / table content.
What's the updated reset version for Django 1.5?
I think what you are looking for is South. South is a 3rd party tool, which may soon be integrated into Django, that assists you with database migrations and schema changes. As is stands, Django 1.5 does not deal with schema changes very well, if at all. The only way to adjust a schema in Django 1.5 is to add new models. You wouldn't want to engage in the practice of adding a new model to fulfill a desired table alteration or deletion. Most developers turn to a 3rd part solution when they need to make schema adjustments.
See http://south.readthedocs.org/en/latest/about.html
See Tutorial http://south.readthedocs.org/en/latest/tutorial/part1.html#tutorial-part-1
South is database agnostic and deals with database migrations automatically for you. So if you change your schema it will detect it in models.py and make the appropriate changes. You can include South as an app to your Django project and install it through pip
Hope this helps
As a MySQL alternative, you can create a application.py next to manage.py with the following code.
This will DROP, CREATE, and UPDATE your database with your models.py
#!/usr/bin/python
import MySQLdb
import subprocess
dbname = "mydbname"
db = MySQLdb.connect(host="127.0.0.1", user="username", passwd="superpassword", db=dbname)
cur = db.cursor()
#Drop all database to Drop all tables
cur.execute("DROP DATABASE "+dbname)
#Recreate the DB
cur.execute("CREATE DATABASE "+dbname)
#Sync with manage.py
proc = subprocess.call(['python','manage.py','syncdb'])
print "\n\nFinished!"
I have a strnage problem.
My django project has myapp module/application. My project uses south to do the schema migrations.
On localhost i have run ./manage.py schemamigration myapp --initial, then i have run migrate command.
But when in production environment i execute migrate command, this doesn't create the correponding table (of myapp models) in database.
It's strange because if i execute migrate --list, myapp has to migration and they are all marked (with * symbol ).
So, i'm thinking about deleting myapp and recreating it from scratch (with corresponding migrations). Is there better solution?
EDIT:
i have tried to delete myapp and to recreate it from scratch. So i have also delete tables of myapp in database (on localhost and on production server), and after all i have executed:
schemamigration myapp --initial command on localhost
migrate myapp command on localhost
migrate myapp 0001 --fake on production server
but South continues to not create the tables of myapp in database of production server.
If you accidentally or intentionally dropped a table in your DB and your are trying to run ./manage migrate myappThis will not create the dropped table in your DB.
Because South does not touch base with your DB.
In case you want to re-create your table. Migrate your schema to a previous version and migrate it latest. Please use the below code accordingly
manage.py migrate myapp 0002 --fake
manage.py migrate myapp
note: 002 is your previous migration version.
if you deleted your tables you shouldn't be running --fake unless you did a manage.py syncdb first. With no table, you should be able to run python manage.py migrate myapp and be done with it (or a manage.py syncdb). The first migration created by --initial has create table statements in it.
--fake explicitly tells south to not do anything but pretend it migrated (performed DB changes) and mark the history table as such.
I know its a bit late, but had this same problem and I found that the problem was that my manage.py was pointing to the wrong settings file hence wrong DB. Ensure your manage.py is pointing to the correct settings file and migrations are being made to the correct DB. This can arise if you are using multiple manage.py files or multiple settings files.
I have made changes to my model.py in Django and now I want to syncronize these changes. It's fine to delete the tables and re-create them. However, nothing seems to work. I am using sqlite3:
syncdb: only works first time, not with changes
"python manage.py sql my_site", followed by syncdb: I thought this would 'redo' it all, but the table still only has the old columns (or so I assume as I get an error when I try to access the table using my model).
Then I figure that I can access the database directly and delete the tables that way. However, I don't know how to get "in" to the DB where I can execute commands. Typing sqlite3 from the command prompt is not recognized. I also tried "python manage.py sql my_site", but I again get the message that sqlite3 is not recognized.
Suggestions?
First you have to install the command line tool for sqlite. On Ubuntu/Debian, you can simply do
sudo apt-get install sqlite3
On windows, you can download it from here: http://www.sqlite.org/download.html. Look for the one that looks like sqlite-shell-win32-xxx.zip.
Use it like this:
> sqlite3 /path/to/your/database
;show some help
.help
; list all databases
.databases
; clear the contents of a table
DELETE FROM <tablename>;
See also the command line reference: http://www.sqlite.org/sqlite.html
and the sqlite SQL reference: http://www.sqlite.org/lang.html.
Using the "ALTER TABLE" sql command, you can also add columns without deleting the entire contents of the table. To do this, compare the output of .schema in sqlite3, and the output of manage.py sql my_site to find out which columns you need to add.
An example:
ALTER TABLE "buildreport_series" ADD COLUMN "parameters" text
Use Django's built in database management tool:
python manage.py dbshell
And issue the required sql commands. The sql command will only print to stdout what the required sql is to create the current tables (as defined by the current models).
It there a tool for django to install some static data (necessary data to have application runing ) to database?
./manage.py syncdb will make the database schema i db, some tool for pushing static data to db ?
Thanks
Fixtures.
EDIT: Better example.
Fill your database with static data you need, then execute command:
./manage.py dumpdata --indent=4 > initial_data.json
After that every ./manage.py syncdb will insert data that you entered the first time into database.
If you are familiar with json format (or xml or yaml) you can edit initial data by hand. For details check out official documentation.