I changed the database from SQLite3 to PostgreSQL in my Django project. Is it possible to store my new database in the GitHub repository so that after cloning and running by the command
python manage.py runserver
the project has started with the whole database?
You cannot save the database as such, you can instead create fixtures to be run. Whenever someone will clone the project, he/she can simple run those fixtures to populate the database.
Related
Setting up a mysql database from an existing dump is very easy
# open prompt cd into the directory where the dump is located
# login to mysql
create database mydb
use mydb
source mydb.sql
And all is done!
But the problem comes with Django. Django has troubles with existing schemes and data that have not been created from models and migration system. And in Django, everything must come from models and migrations.
I perfectly know that I have this option
python manage.py inspectdb > models.py
But this is only suitable when creating a blank new Django project using existing database.
What if I want to run an existing Django project to another machine and the Django project uses a database? The project already has existing models with some manage=false set, I cannot simply inspectdb it. And if I restore the database outside Django, the Django ORM will no longer recognize it.
Then what is the correct way to restore a database from a database backup for a project with existing models and migrations?
If the database that you are restoring was previously managed by django, you should encounter no problems simply restoring the database with your three-step process. Django's migration history is stored as a table in any database that it manages. As long as that table makes it over, your new migrations going forward should work fine.
I have an existing django project and need to create an instance of it in a new environment with new database. I have the database connection configured in the settings file. The schema does not exist. If I run the manage.py migrate command, does it also create the schema? It looks like it assumes the schema already exists because I am getting an error django.db.utils.OperationalError: (1049, "Unknown database 'my_db'"). Just wondering if I have to create the database first or if some django command is available to create it if it does not exists.
I can create the schema manually via sql script if it's not doable via python django command.
As already pointed out in the comments to your question, the missing database is the problem, not the schema. You have to create the database first, which might involve setting the user permissions for the new database. After that, the manage.py migrate command will work just fine and create the schema for you.
Django v1.11
Postgresql v9.6
Currently, I use 2 Google CloudSQL databases, one for development and one for production. Whenever I make changes to my models, I run python manage.py migrate to update the tables in the development database. This migration does not affect the production database, however.
Now, whenever I git push changes to my Django project, TravisCI automatically runs tests and deploys the code to Google App Engine. Currently, it runs on GAE flexible environment (so I can use Python 3.5)
What I want to have is for Travis or GAE to automatically run python manage.py migrate on the production database before runserver. However, I can't figure out how to run custom commands during a deploy.
I've tried looking around GAE and Travis documentation and adding scripts to .travis.yml and app.yaml, but to no avail.
As of now, anytime there is a model change, I have to migrate the production database locally in a very hacky way. Ideally, GAE will migrate at the beginning of every deploy.
Not sure if you have seen this:
Travis CI Script Deployment
A reference from a similar issue:
How can I run a script as part of a Travis CI build?
Also, consider a database migration tool embedded with your source code, Postgresql is supported (something similar to FlywayDB migration ):
Yoyo database migrations¶
So I have my Django app running and I just added South. I performed some migrations which worked fine locally, but I am seeing some database errors on my Heroku version. I'd like to view the current schema for my database both locally and on Heroku so I can compare and see exactly what is different. Is there an easy way to do this from the command line, or a better way to debug this?
From the command line you should be able to do heroku pg:psql to connect directly via PSQL to your database and from in there \dt will show you your tables and \d <tablename> will show you your table schema.
locally django provides a management command that will launch you into your db's shell.
python manage.py dbshell
django also provides a management command that will display the sql for any app you have configure in your project, regardless of the database manager (SQLite, MySQL, etc) that you're using:
python manage.py sqlall <app name>
Try it! It could be usefull!
I had a django project with one database (default). South was installed for generating migration scripts and nose as a test framework. Models were built on this database. All tests were run successfully.
Subsequently, I needed to connect a second database (legacy), which I also added to the DATABASES configuration. I access this database using raw sql and no models. While trying to run the previously running tests, I noticed that:
nose also creates a test database for the legacy database
default django tables (auth_... etc) are also created in this database
south runs all migration scripts against the legacy database as well and fail to do so
What I would like is to disable the creation of the test legacy database and the running of the migration scripts on it. Ideally, I would like to create tables in the test legacy database myself by issuing raw sql create-insert statements. Is that possible?
Thank you for any help.
Your path of least resistance is probably to write your own test running management command. To do so you can either override the existing command or simply create a separate command with your desired behavior.
The docs for creating custom management commands can be found on the official django docs and you can find a decent example of overriding the stock "test" command in the django-test-extensions project.