I try to deploy my website developped with Django 1.9. I use the web hosting AlwaysData.
The problem is that the database of my web hosting is empty (no tables) and I want to import my tables (in models.py) in this database.
For that, I need a file sql that contains all my tables. After I will import this file in my AlwaysData database.
I try the command dumpdata but the format sql doesn't exist (only yaml, xml and json).
How can I do ?
I use PostGreSQL.
Thank you
I would think that you could build your database using ./manage.py makemigrations and then ./manage.py migrate. However, if that does not work for you, it sounds like you want the sqlmigrate management command:
./manage.py sqlmigrate my_app my_migration > my_migration.sql
This will generate the SQL for your migration, and redirect it to a file called my_migration.sql.
Related
I'm working in Django 1.9, and I'm trying to get the SQL that would be used to create database tables from models.py.
It seems that in previous version of Django one could do:
python manage.py sqlall <app_name>
But that no longer seems to be the case. I just get Unknown command: 'sqlall'. Nothing in the docs seems to offer this.
How can I see how my models are turned into SQL?
It is deprecated in Django 1.9:
The SQL management commands for apps without migrations, sql, sqlall,
sqlclear, sqldropindexes, and sqlindexes, will be removed.
The closest command you can use is sqlmigrate:
Prints the SQL for the named migration. This requires an active
database connection, which it will use to resolve constraint names;
this means you must generate the SQL against a copy of the database
you wish to later apply it on.
You can use the migration name 0001_initial for the creation script, for example:
python manage.py sqlmigrate myapp 0001_initial
I found that using sqlmigrate was a pretty rotten solution for me. I'm setting up logical replications, and so I need the SQL of the database, not the SQL of individual migrations.
The solution I found was to use pg_dump with the --schema-only option.
What's the best way to deploy custom SQL for views used by unmanaged Django models?
I have a model myapp.models.MyModel using a view myview.sql, specified in the models "db_table" meta attribute. So, following the docs, I placed the custom SQL file in myapp/sql/myview.sql.
However, after I run python manage.py syncdb, the view is not installed. Running sqlcustom similarly does not show the view.
What else do I need to do so Django will automatically detect and deploy my custom SQL?
python manage.py sqlcustom will only output the custom SQL statements that you've defined, but you need to execute them in the database. Of course, you can copy and paste, but if you want to automate the process and have it be less tedious/error-prone, one easy way to do so (at least on a *nix environment) is to use a pipe:
python manage.py sqlcustom myapp | python manage.py dbshell
According to the doc, name the file myapp/sql/mymodel.sql instead of myapp/sql/myview.sql
I am using Django to build a website with MySQL. Now as I am learning so I need to change the Model very often so I want that all tables get cleared and new table get created.
But syncdb doesn't touch existing tables. Is there any better way to handle this problem?
If you don't care about data:
Best way would be to drop the database and run syncdb again. Or you can run:
For Django >= 1.5
python manage.py flush
For Django < 1.5
python manage.py reset appname
(you can add --no-input to the end of the command for it to skip the interactive prompt.)
If you do care about data:
From the docs:
syncdb will only create tables for
models which have not yet been
installed. It will never issue ALTER
TABLE statements to match changes made
to a model class after installation.
Changes to model classes and database
schemas often involve some form of
ambiguity and, in those cases, Django
would have to guess at the correct
changes to make. There is a risk that
critical data would be lost in the
process.
If you have made changes to a model
and wish to alter the database tables
to match, use the sql command to
display the new SQL structure and
compare that to your existing table
schema to work out the changes.
https://docs.djangoproject.com/en/dev/ref/django-admin/
Reference: FAQ - https://docs.djangoproject.com/en/dev/faq/models/#if-i-make-changes-to-a-model-how-do-i-update-the-database
People also recommend South ( http://south.aeracode.org/docs/about.html#key-features ), but I haven't tried it.
Using Django Extensions, running:
./manage.py reset_db
Will clear the database tables, then running:
./manage.py syncdb
Will recreate them (south may ask you to migrate things).
I think Django docs explicitly mention that if the intent is to start from an empty DB again (which seems to be OP's intent), then just drop and re-create the database and re-run migrate (instead of using flush):
If you would rather start from an empty database and re-run all
migrations, you should drop and recreate the database and then run
migrate instead.
So for OP's case, we just need to:
Drop the database from MySQL
Recreate the database
Run python manage.py migrate
Quickest (drops and creates all tables including data):
./manage.py reset appname | ./manage.py dbshell
Caution:
Might not work on Windows correctly.
Might keep some old tables in the db
You can use the Django-Truncate library to delete all data of a table without destroying the table structure.
Example:
First, install django-turncate using your terminal/command line:
pip install django-truncate
Add "django_truncate" to your INSTALLED_APPS in the settings.py file:
INSTALLED_APPS = [
...
'django_truncate',
]
Use this command in your terminal to delete all data of the table from the app.
python manage.py truncate --apps app_name --models table_name
just wondering... but is it possible to generate SQL commands for django's models?
e.g. the session model
python manage.py sql django
Any ideas?
You need to mention the name of the specific app that comes with django, such as
python manage.py sql auth
python manage.py sql admin
I find it a bit clumsy that you can't give fully-qualified names (such as django.contrib.auth), but using just the last part of the name seems to work fine.
This is no longer possible with django migrations (django>=1.7). The sql management command is no longer available.
Though it operates slightly differently, the sqlmigrate management command can be used to find the sql commands for individual migrations.
./manage.py sqlmigrate auth 0001
./manage.py sqlmigrate auth 0002
...
I've already defined a model and created its associated database via manager.py syncdb. Now that I've added some fields to the model, I tried syncdb again, but no output appears. Upon trying to access these new fields from my templates, I get a "No Such Column" exception, leading me to believe that syncdb didn't actually update the database. What's the right command here?
As of Django 1.7+, built-in migrations support, allows for database schema migrations that preserve data. That's probably a better approach than the solution below.
Another option, not requiring additional apps, is to use the built in manage.py functions to export your data, clear the database and restore the exported data.
The methods below will update the database tables for your app, but will completely destroy any data that existed in those tables. If the changes you made to your app model do not break your old schema (for instance, you added a new, optional field) you can simply dump the data before and reload it afterwards, like so:
Django 1.4.15 and earlier
python manage.py dumpdata <your_app> > temp_data.json
python manage.py reset <your_app>
python manage.py loaddata temp_data.json
Django 1.5 and newer
python manage.py dumpdata <your_app> > temp_data.json
python manage.py sqlclear <your_app> | python manage.py dbshell
python manage.py syncdb
python manage.py loaddata temp_data.json
(The reset command was deprecated and then removed in Django 1.5)
If your changes break your old schema this won't work - in which case tools like South or Django Evolution are great.
As of Django 1.7, you can now do this with native migrations. Just run
python manage.py makemigrations <your app name>
python manage.py migrate
Seems like what you need is a migration system. South is really nice, working great, has some automation tools to ease your workflow. And has a great tutorial.
note: syncdb can't update your existing tables. Sometimes it's impossible to decide what to do automagicly - that's why south scripts are this great.
Django's syncdb doesn't alter existing tables in the database so you have to do it manually. The way I always do it is:
Change the model class first.
Then run: manage.py sql myapp.
Look at the sql it prints out and see how it represented the change you are going to make.
Make the change manually using your database manager.
Check to see if everything worked correctly using the admin site.
If you are using sqllite a good manager is the firefox plugin: link
Another tool would be django evolution. No table dropping needed in most cases.
django evolution
Just install it as any other django app and run:
python manage.py evolve --hint --execute
deseb is a great tool for that.
Having it installed, you can write ./manage.py sqlevolve and it'll generate sql commands necessary to keep the database structure in sync with your models.
You need to drop your tables before you can recreate them with syncdb.
If you want to preserve your existing data, then you need to unload your database,
drop your tables, run syncdb to build a new database, then reload your old data into your new tables.
There are tools that help with this. However, in many cases, it's just as easy to do it manually.
For versions 1.4.1 and above users the command has changed to
python manage.py flush
Please read the official document before using it as it will delete all your data.