What should I use instead of syncdb in Django 1.9? - python

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

Related

Why should we use three migration commands to migrate our database succesfully?

I was working on models in django and after creating classes for models and when migrating them I was supposed to use three commands for completing migration of models. The commands are :python manage.py migrate,python manage.py makemigrations <app_name>,python manage.py migrate
So my question is that why should we use three commands like this ?
From the django docs,
migrate: which is responsible for applying and unapplying migrations.
makemigrations: which is responsible for creating new
migrations based on the changes you have made to your models.
If you make changes to your model use python manage.py makemigrations followed by python manage.py migrate.
Incase of initial migration just after you create an app you dont need to python manage.py makemigrations but just python manage.py migrate works.

Django migrating app without migrations

I've created a new Django project consisting of a single app. I thought Django would only migrate an app only if its migrations exist.
The first time I run python manage.py migrate, Django would create tables for my app as well(I've created models.py for my app)
I haven't run python manage.py makemigrations app or python manage.py makemigrations till this time.
I would get the following output on running python manage.py migrate:
Synchronizing apps without migrations:
Creating tables...
Creating table app_model
This is not what I want, because the next time I actually make migrations for the app and run migrate, Django would complain that the tables already exist.
You can create the migrations, and fake the first migration:
python manage.py makemigrations <app_name>
python manage.py migrate --fake-initial
This will skip any initial migration for which the tables have already been created.
Django (pre 1.9) will synchronize any apps that don't have migrations, see the docs on migrate:
The behavior of this command changes depending on the arguments provided:
No arguments: All migrated apps have all of their migrations run, and all unmigrated apps are synchronized with the database,
If you want to save migrations for app, wich you already migrate. You can change one field in model, then makemigrations this_app -> migrate and return has been again with makemigrations and migrate. But I dont understand why you want this.

makemigrations does not work after deleting migrations

I mistakenly deleted all the .py file under path projectName/appName/migrations, it include like:
0001_initial.py
0011_auto_20150918_0723.py
0002_auto_20150819_1301.py
...
Now, even I updated the Model file, and run the command python manage.py makemigrations, it always prompt with No changes detected.
How can I recover everything?
First of all, even if this happened to you in production, do not panic.
When you deleted all migrations django forgot that this app is supposed to be managed by migrations. Django defaults back to the legacy python manage.py syncdb migrationless behaviour and will not attempt to detect changes or generate new migrations when you run python manage.py makemigrations
In order to make it aware of the migrations, you have to run the command specifically for your app:
python manage.py makemigrations appName
However, for your running application, django will not be able to detect that the new migrations were already applied in the database, and will try to run them again when you run python manage.py migrate.
When this happens the migrations fail saying that the relation appName.XYZ already exists!.
To make django understand that your migration is already reflected in the database you have to fake them:
python manage.py migrate appName --fake
This will update the migration history table and mark your migrations as applied, but will not attempt to create the tables in the database.
Update (thanks to #GwynBleiD):
One concern here is that deleted migrations which were already applied, will be listed in the migrations history table in the database. This will not be a problem for the initial python manage.py makemigrations myApp, however for any other migrations it will raise an error about inconsistent migration history.
To avoid that, you must remove by hand any row from django_migrations table in database that refers to nonexistent migrations.

How can I resolve 'django_content_type already exists'?

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.

How do you remove all data after running syncdb?

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

Categories

Resources