I used to use manage.py sqlall app to dump the database to sql statements. While, after upgrading to 1.8, it doesn't work any more.
It says:
CommandError: App 'app' has migrations. Only the sqlmigrate and
sqlflush commands can be used when an app has migrations.
It seems there is not a way to solve this.
I need to dump the database to sql file, so I can use it to clone the whole database else where, how can I accomplish this?
You can dump the db directly with mysqldump as allcaps suggested, or run manage.py migrate first and then it should work. It's telling you there are migrations that you have yet to apply to the DB.
Try the following:
python manage.py dumpdata <app_name> > <outputfile>
Related
I am getting error like django.db.utils.OperationalError: (1050, "Table 'someTable' already exists")
I want to know reason for getting this type error.
I ran the following commands on termial
1. python manage.py makemigration app
2. python manage.py migrate app
When ran migrate then getting the above error.
I solve my problem by running
python manage.py migrate --fake app
But I want to know why I am getting this error, and how --fake app solve my problem. Thanks
The table 'someTable' already exists in your database - either because it's been created by a previous call to ./manage.py syncdb or because you created it manually (or you used South before and are switching to Django >= 1.7) - and you obviously didn't have any existing django (non-south) migration, so makemigration thinks the table has to be created (rightly so sonce that will indeed be the case for someone that install your app from scratch).
Using the --fake flag tells migrate command to just record the migration has having been applied without effectively applying it, and that's the whole point of this flag: when your app has already been installed (db tables etc) without migrations and you want to start using migrations.
This problem means that someTable was created without Django migrations or record about migrations in django_migrations was deleted. --fake adds record about migrations in django_migrations without applying actual migrations.
Record in django_migrations contains information about migration: app label, migration name and date when migrations was applied.
I had the same problem. I was using the MySQL database.
With SQLite, you will need to delete the SQLite.db
All I did when using MySQL as database to handle this was...
make migrations python manage.py makemigrations
drop the database from the mysql shell mysql> drop database [database name];
create the database from the mysql shell mysql> create database [database name];
migrate using the python manage.py migrate --run-syncdb command
After upgrading to django 1.8 I'm recieving the error during migration:
ProgrammingError: relation "django_content_type" already exists
I'd be interested in the background behind this error, but more importantly,
How can I resolve it?
Initial migrations on a project can sometimes be troubleshot using --fake-initial
python manage.py migrate --fake-initial
It's new in 1.8. In 1.7, --fake-initial was an implicit default, but explicit in 1.8.
From the Docs:
The --fake-initial option can be used to allow Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names and so is only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.
https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial
I solved this issue on Django 2.2.7 or Django 3.0 hosted on Ubuntu 18.04 + Postgres 10.10 version.
Restore the database in Postgres database (used pgAdmin tool for this)
(virtualenv)python manage.py loaddata dumpfile.json
Dropping django_migrations table from database (used pgAdmin tool for this)
(virtualenv)python manage.py makemigrations
(virtualenv)python manage.py migrate --fake
(virtualenv)python manage.py migrate
(virtualenv)python manage.py collectstatic
(virtualenv)python manage.py runserver 0.0.0.0:8000
I granted all privileges to the user on that specific database and it solved the issue.
Take a look at this:
$ pypy ./manage.py syncdb
/usr/lib64/pypy-2.4.0/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
(cut)
I ran a quick google search, but could not find the answer - what should I be using instead of syncdb?
syncdb is deprecated because of the migration system, introduced with django 1.7.
Now you can track your changes using makemigrations. This transforms your model changes into python code to make them deployable to another databases. When you have further modifications you need applied to the database, you can use data migrations.
After you created the migrations you have to apply them: migrate.
So instead of using syncdb you should use makemigrations and then migrate.
Workflow on development after you changed something in your models:
./manage.py makemigrations
./manage.py migrate
And on your production system:
./manage.py migrate
Bonus: you do not need to run migrate for each change. If you have multiple changes not applied yet django will run them in the correct order for you.
You should definitely use migration system. Which lets you track changes in your models.py, and create migrations for the database. The migration system uses the commands makemigrations to create migrations and migrate to migrate the database.
If for whatever reason you need to create a database the same way syncdb did it there is command flag that causes migrate to work the same way. You should only do this if you REALLY need it and you know what you are doing. For example to create an empty database on for a continuous integration system of your choice.
python manage.py migrate auth
# performs migrations for auth and contenttypes contrib apps
python manage.py migrate --run-syncdb
# creates the rest of the database
Tested on Django 1.9.1.
You should use the makemigrations and migrate commands that were introduced in django 1.7
https://docs.djangoproject.com/en/1.7/topics/migrations/
syncdb has some problem with db migration. so, after django 1.7 makemigrations and migrate have been introduced.
Now in django 1.9 syncdb has been deprecated.
try
1. python manage.py makemigrations which detects changes in db and creates one .py file as inside migrations folder
2. python manage.py migrate will apply the migrations to the database
I'm in production and just ran syncdb but I made a mistake and want to delete what syncdb did, including all data in the tables is fine. Just a fresh start in the databases so I can run syncdb again.
(virtualenv-2.7)[root#server mysite]# python manage.py sqlclear mainapp | python manage.py dbshell
CommandError: One or more models did not validate:
app.comment: 'poster' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.
app.myuser: The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.
Recreate database is probably the best way. Also you can do it using reset_db management command from django-extensions package.
The Django 1.7 support migrate
you just run python manage.py migrate again after edit.
more features to see: release note
As J0HN stated in the comments, the best thing to do to start fresh was to actually just delete the database. I thought it might mess something up, but it won't. Simply login to mysql.
drop database whatever_your_database_name;
create databse whatever_your_database_name;
Then simply sync it again with python manage.py syncdb
Maybe its lazy, but I run:
django-admin.py reset_db --router=default --noinput
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!