now im trying to migrate empty django project.
when enter this command
python manage.py showmagrations
result is
admin
[ ] 0001_initial
[ ] 0002_logentry_remove_auto_add
[ ] 0003_logentry_add_action_flag_choices
auth
[ ] 0001_initial
[ ] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002
[ ] 0007_alter_validators_add_error_messages
[ ] 0008_alter_user_username_max_length
[ ] 0009_alter_user_last_name_max_length
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
myapp
[ ] 0001_initial
sessions
[ ] 0001_initial
is this situation how can i migrate all of 0001_initial?
or how can i run spesific migrtaion step?
ex) admin 0001_initial only
it's simple you have only put the app name and the migration you want to migrate
if you want to migrate the inital of the admin app:
$ python manage.py migrate admin 0001_initial
NOTE: it will unapply the later migrations
Related
I have a django project, and I wish to update a MySQL database from a previous version of the codebase to be compatible with the current version.
I have all of the migrations for the project, but I do not know which was the last one applied to this alternate database.
I can try and determine this manually, but I was wondering if there was an easier automated way of finding which the first unapplied migration is.
You can work with the showmigrations command [Django-doc], this will generate a list of the migrations where it will check a box in case that migration has been applied, so:
python3 manage.py showmigrations
or for a specific app:
python3 manage.py showmigrations app_name
For example for auth, a possible result is:
$ python manage.py showmigrations auth
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[ ] 0011_update_proxy_permissions
[ ] 0012_alter_user_first_name_max_length
This thus means that all migrations have been applied, except the last two.
Django keeps making duplicate migrations in for my application. I've ran makemigrations and migrate before I changed my model. After the changes I ran makemigrations again to make migrations for the updated model, I got the following error:
CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0001_initial, 0001_initial 3 in sessions; 0002_remove_content_type_name 3, 0002_remove_content_type_name, 0001_initial 3 in contenttypes; 0002_alter_permission_name_max_length 3, 0010_alter_group_name_max_length 3, 0007_alter_validators_add_error_messages 3, 0006_require_contenttypes_0002 3, 0005_alter_user_last_login_null 3, 0001_initial 3, 0008_alter_user_username_max_length 3, 0009_alter_user_last_name_max_length 3, 0011_update_proxy_permissions, 0011_update_proxy_permissions 3, 0004_alter_user_username_opts 3, 0003_alter_user_email_max_length 3 in auth; 0003_logentry_add_action_flag_choices 3, 0002_logentry_remove_auto_add 3, 0003_logentry_add_action_flag_choices, 0001_initial 3 in admin).
To fix them run 'python manage.py makemigrations --merge'
Running makemigrations --merge doesn't work: ValueError: Could not find common ancestor of {'0002_remove_content_type_name', '0002_remove_content_type_name 3', '0001_initial 3'}
There are many duplicate migrations in apps that I haven't touched (auth, admin, etc.):
admin
[ ] 0001_initial 3
[X] 0001_initial
[ ] 0002_logentry_remove_auto_add 3
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
[ ] 0003_logentry_add_action_flag_choices 3
auth
[ ] 0001_initial 3
[X] 0001_initial
[ ] 0002_alter_permission_name_max_length 3
[X] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length 3
[X] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts 3
[X] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null 3
[X] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002 3
[X] 0006_require_contenttypes_0002
[ ] 0007_alter_validators_add_error_messages 3
[X] 0007_alter_validators_add_error_messages
[ ] 0008_alter_user_username_max_length 3
[X] 0008_alter_user_username_max_length
[ ] 0009_alter_user_last_name_max_length 3
[X] 0009_alter_user_last_name_max_length
[ ] 0010_alter_group_name_max_length 3
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
[ ] 0011_update_proxy_permissions 3
contenttypes
[ ] 0001_initial 3
[X] 0001_initial
[X] 0002_remove_content_type_name
[ ] 0002_remove_content_type_name 3
database
(no migrations)
sessions
[X] 0001_initial
[ ] 0001_initial 3
The only app that, as far as I know, should've been affected is database. Why is Django making these duplicate migrations? Is there any way I can get rid of these? Or apply them so they are not blocking anymore? How I can make sure this doesn't happen again?
Sometimes, if you have the option, the easiest and fastest way is...
Delete migration files.
Delete entire database.
run "python manage.py makemigrations" again
run "python manage.py migrate" again
or... if you have a db in production with data and you can't reset:
Check last production version
Delete migration files (until this version).
Delete entire local(sqlite) database.
run "python manage.py makemigrations" again
run "python manage.py migrate" again
Important... copy /migrations/ folder somewhere to recover files if necessary.
It turned out the duplicates weren't created by Django but by some other process. Completely removing my environment and reinstalling all dependencies helped. Removing the migrations also would've worked.
showmigrations shows that there is 31 available migrations:
# python3 manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_auto_20190114_1409
[X] 0003_auto_20190114_1410
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_auto_20190114_1409
authtoken
[X] 0001_initial
[X] 0002_auto_20160226_1747
[X] 0003_auto_20190114_1409
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
[X] 0003_auto_20190114_1409
exchange_delivery
[X] 0001_initial
[X] 0002_enlarge_phone_field
[X] 0003_unique_external_id
[X] 0004_add_warehouse
[X] 0005_add_delivery_point_type
[X] 0006_update_delivery_type_operating_mode
[X] 0007_add_delivery_point_region_model
[X] 0008_update_warehouse_exchange
[X] 0009_add_verbose_name_for_warehouse_and_add_delivery_point_banned_group
[X] 0010_add_active_flag_to_warehouse
[X] 0011_auto_20190114_1409
sessions
[X] 0001_initial
[X] 0002_auto_20190114_1410
volt
[X] 0001_initial
[X] 0002_auto_20190114_1410
How to apply all this migrations ? migrate shows that No migrations to apply
# python3 manage.py migrate
Operations to perform:
Synchronize unmigrated apps: corsheaders, export, opinion, volt.integration1c.delivery, custom_logger, event_listener, legacy, region, order, catalog, promo_table, messages, staticfiles, api, best_product, shop, general, market_cpa, rest_framework, delivery, exchange, talk
Apply all migrations: auth, authtoken, sessions, exchange_delivery, admin, volt, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
/usr/lib/python3.6/site-packages/django/core/management/commands/loaddata.py:239: RemovedInDjango19Warning: initial_data fixtures are deprecated. Use data migrations instead.
RemovedInDjango19Warning
Installed 0 object(s) (of 6) from 1 fixture(s)
Running migrations:
No migrations to apply.
I'm not familiar with Django, I need just dockerize existing app.
As far as I understand from output of python3 manage.py syncdb command
django.db.utils.ProgrammingError: (1146, "Table '220-django.auth_user' doesn't exist")
Problem is that initial migrations for django and django-admin are not applied
Project is under Django 1.8.14
As I can see, all the migrations has [X] beside their names. Means these migrations are already applied, as per documentation. If there is any un-applied migrations, then it will show up as [ ] beside the name of the migration. For more details(if the migrations has been applied or not), please check the django_migrations table in DB.
You have some entries in django_migrations table in your database so that migrate shows No migrations to apply.
To solve this, goto database console and run the following command
delete from django_migrations;
Or, directly go to the database table and delete all rows.
Then run
python manage.py migrate
I've searched every Stack Overflow question on this error but none of the responses helped. I'm getting this error when trying to access the admin page of this particular model (AgentBasicInfo).
'manage.py makemigrations' works fine. 'manage.py migrate' also works fine.
'manage.py runserver' works fine, the whole website works fine until I try to go onto the admin page of this model.
The app is correctly installed in INSTALLED_APPS in settings.py. I am using Postgres for the database.
I have tried...
Deleting migrations and re-running makemigrations/migrate
Deleting the entire migrations folder for this app and rerunning makemigrations/migrate
Deleting all the migrations from all my apps and re-running makemigrations/migrate
I have tried running 'manage.py migrate' and 'mangae.py migrate app_name'. I still get the same error.
This model (see code below) is quite basic. I have several other models in my project and they work just fine in the admin, but just this particular model doesn't work.
models.py
class AgentBasicInfo(models.Model):
preferred_email = models.EmailField()
office_phone_number = models.IntegerField()
brokerage_of_agent = models.CharField(max_length=50)
agent_title = models.CharField(max_length=20)
def __str__(self):
return self.preferred_email
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'lagger123',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
0001_initial.py
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='AgentBasicInfo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('preferred_email', models.EmailField(max_length=254)),
('office_phone_number', models.IntegerField()),
('brokerage_of_agent', models.CharField(max_length=50)),
('agent_title', models.CharField(max_length=20)),
],
),
]
Output of manage.py showmigrations:
accounts
[X] 0001_initial
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
coresite
(no migrations)
databases
(no migrations)
manage_listings
[X] 0001_initial
search_listings
(no migrations)
sessions
[X] 0001_initial
teams
(no migrations)
Open db command line.
python manage.py dbshell
And try this
delete from django_migrations where app='app_name';
Then delete migration files and run migration commands.
I also had this problem and tried:
python manage.py dbshell
But then I got this error:
CommandError: You appear not to have the 'psql' program installed or on your path.
This was due windows not finding psql in my environment path.
As an alternative, you can get it done by reverting changes (that is if you had previous changes in you git repository.
For me I used this method:
git checkout <commit hash> (which did not have the error)
After that pull the changes:
git pull <remote> <branch>
Then finally:
git push origin main
Hope this helps for the ones with git repositories. I welcome any corrections.
I am working on a Django(1.8) project.
I am trying to implement django-seo2.
Integrated Travis CI on GitHub for continuous integration.
I keep getting the following error on Travis:
ProgrammingError: relation "django_content_type" does not exist
On my local machine:
python manage.py showmigrations
account
[ ] 0001_initial
[ ] 0002_email_max_length
admin
[ ] 0001_initial
auth
[ ] 0001_initial
[ ] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002
authtoken
[ ] 0001_initial
[ ] 0002_auto_20160226_1747
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
payment
[ ] 0001_initial
profiles
[ ] 0001_initial
[ ] 0002_auto_20160610_1309
sessions
[ ] 0001_initial
sites
[ ] 0001_initial
socialaccount
[ ] 0001_initial
[ ] 0002_token_max_lengths
[ ] 0003_extra_data_default_dict
webpages
(no migrations)
If I migrate before implementing django-seo2 and then I migrate after implementing django-seo2 no error occurs.
But if new database is used and after implementing django-seo2, I try python manage.py migrate , the same error occurs:
django.db.utils.ProgrammingError: relation "django_content_type" does not exist
The version on pypi is 12 commits behind master.
I solved the problem by installing the django-seo2 directly from its source on github
I created an issue for update in django-seo2 Python Package Index, but I think the github repository is not maintained by the owner now.