Reset Django registration models? - django

I am trying to implement django registration-redux 1.2. I installed the application and added to it settings.py of my project. I ran manage.py syncdb as well as makemigrations/migrate. Typing these commands again and I get no changes detected. However it seems like the tables are not getting created. When I try to register I get the following error:
ProgrammingError at /main/register/ (1146, "Table 'la_test_serve.registration_registrationprofile' doesn't exist")
Is there a way to reset the project/app so that these tables get created?
Thanks,
Robert

Try to run schemamigration for your registration apps
python manage.py schemamigration registration --initial
after that run migrate
python manage.py migrate registration

Remove the app from your "INSTALLED APPS" setting. Then run manage.py makemigrations and manage.py migrate. Reinstall the app.
Note: If you didn't add 'registration' (yes, simply 'registration') to your "INSTALLED APPS", it won't work.

I found this error occurred when I was reinstalling django-registration-redux.
Either way, check that you have deleted not only the table for registration in the database but also ensure that in the migrations table delete the corresponding row, in this case 'registration'.

Related

relation "django_admin_log" already exists

when i try to run python manage.py migrate i run into following error
Upon running python manage.py run migrations it says no changes detected. and when i runserver it gives me warning that i have unapplied migrations as well.i have been searching internet for two hours but got not solution. Someone knowing the solution please share :)
The table in your database that stores migration data to keep track of what has been applied is out of date. Try running python manage.py migrate --fake
Try python manage.py makemigrations [app name] and if still, this does detect changes then delete the folder named migrations which is inside your application folder and then use this python manage.py makemigrations [app name]. Once migration happens successfully do the python manage.py migrate.
Don't Try This at Home
I faced this issue, i make two changes,
change AUTH_USER_MODEL, so i have one migraiton about it
second one add new field for my folder_model(migration name: folder_model 0021)
When my first migrate attempt(I already run makemigrations commands on local so i have migration files), it says;
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'
This error reiases because i change the AUTH_USER_MODEL in the middle of the project, normally you have to remove your database and fresh start from the beginnig(also truncate migrations etc.), according to Django doc -> https://code.djangoproject.com/ticket/25313
To fix this issue, you don't have to delete all migrations on db, just delete the migrations about admin(not from project just database)
After that just run
python manage.py migrate
It throws relation "django_admin_log" already exists. For this issue, run:
python manage.py migrate --fake
That's it, but not completely. Make fake migration act like you already make your all migrations successfully and save these on db. The issue came here that i have another migration about folder_model 0021 and with fake migration it doesn't applied to my database table but saved to db_migrations table.
So fix this issue, delete the folder_model 0021to database migration table (just 0021 not all folder_model migrations).
After delete just run python manage.py migrate
Everything is fine!

InconsistentMigrationHistory with django user_auth

I'm working on my first django project and I'm trying to add user_authentication now. I know i probably should've done this at the very start but I'm trying to do it now. I have a few other apps created and its running fine. However when I added an accounts app i get the following error when i run migrations in accounts/models.py
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency accounts.0001_initial on database 'default'.
What helped me with migrations in situation where I have problem like:
django.db.utils.ProgrammingError: (1146, "Table 'mydjango.MyApp_publication' doesn't exist").
It's regarded to the connections.py is following:
Run the command python manage.py
migrate --fake MyApp zero
and then python manage.py migrate MyApp.
Here 'MyApp' is the app you're creating on your Django Server.
I delete all tables except ‘auth_user’, and run ‘python manage.py makemigrations myapp’ ‘python manage.py migrate myapp’ again.
This solved the problem.

django migration No migrations to apply

I have screwed up my database so I tried to redo it. I did:
1) deleted all tables via sql
2) deleted the migrations folder
So when I now do manage.py makemigrations myapp it is creating the migration folder and the initial.py file, which looks fine. Also the __init__.py is there. However, if i do manage.py makemigrations myapp I always get the message "No migrations to apply."
I looked up the database in the shell and the tables are not there.
I am using Django 1.8.
Django keeps track of all the applied migrations in django_migrations table.
So just delete all the rows in the django_migrations table that are related to you app like:
DELETE FROM django_migrations WHERE app='your-app-name';
and then do:
python manage.py makemigrations
python manage.py migrate
Django keeps track of all the applied migrations in django_migrations table.
So, you can follow this method:
Delete the related rows from django_migrations.
run python manage.py migrate.
I usually ran into that issue myself. Here is the workaround I found:
Replace your database sqlite3 with this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': '<your_mysql_hostname>',
}
}`
The fill in for can be found in the databases tab on your Pythonanywhere dashboard.
Push it to github from your Terminal and pull it down from Pythonanywhere Bash again. You may have to add this on Bash Console: pip install mysqlclient
As #Anush mentioned, Django does keep track of all of the migrations in the django_migrations table. As mentioned, you can use raw SQL to remove the migration row to reset Django's migration history for the concerned app.
DELETE FROM django_migrations WHERE app='my-app';
If you are uncomfortable deleting rows like this, then you can use Django's management command replacing my-app for the name of your app.
python manage.py migrate --fake my-app zero
That command will remove the migration history rows from the database leaving you to start your migration again from scratch. You will then need to:
Delete all of the migration files within your app's 'migrations' directory but ensure that the __init__.py file remains.
Ensure that your app's data models are set up as you need them
Make the migrations again with the command python manage.py makemigrations {your_app_name}. This will create the 0001_initial.py file within your app's migrations directory.
You will not be able to actually migrate the initial database migration because the database table already exists, so you will need to fake the migration with the command, python manage.py migrate my_app --fake-initial
Instead of run:
python manage.py migrate
just run:
python manage.py migrate --run-syncdb
If you have many problems and require a solution
Create a database backup (more than one if necessary)
My solution was to run the following query in the SQL shell
drop database database_name with (force);
This completely removes the database, be careful
It solved the error for me
Greetings
After running into this problem on Django 3.0 I spent an entire day troubleshooting this. Sharing this so that others would not need to. Most of the times running
python manage.py makemigrations
python manage.py migrate
Should solve the problem. I followed the above step after deleting the files in migrations folder (except init.py), however I was not able to solve the problem. I found that every time you run makemigrations, a python file called 000x_xxx.py is created in the migrations folder. I looked inside the file and found that all my required changes were there but it also had other create table operations that I did not need (i.e those tables had already been created). If you run migrate with --fake or --fake-initial, the whole execution will be faked (i.e executed without actually making any change in the database).
To solve the problem I opened the generated 0000x_xxx.py file and deleted the changes I wanted to make in the database and ran "python manage.py makemigrations" again. This created a new migration file with the changes I wanted. After that I ran the following command.
python manage.py migrate --fake-initial appname
This solved the problem. Not sure if this should be this complicated.

Django 1.7 on Heroku: how do I get makemigrations to rescan the database?

I have Django 1.7 running on Heroku. I've made a change to the models.py file (added a column to a table) but Django doesn't seem to be able to detect this. When I run
python manage.py makemigrations appname
it responds No changes detected in app.
I've tried deleting the appname/migrations folder, but that doesn't help.
Is there a way to get Django to rescan the database and check for differences? This was easy with South.
https://docs.djangoproject.com/en/1.7/topics/migrations/#the-commands
Have you tried
python manage.py migrate
It seems the migrate is "responsible for applying migrations, as well as unapplying and listing their status."

South not Migrating django.contrib.admin

I have a Django project that's managed by South. I ran through the normal procedure when I first started the project:
Install South
Add South to INSTALLED_APPS
Perform initial syncdb
schemamigration --initial, followed by migrate myapp
Everything's been working fine and I've done several migrations since. Now I enabled the django admin portion of the site (INSTALLED_APPS, urls, etc.). When I tried adding a test user, I was received the following error:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/auth/user/add/
Django Version: 1.4.3
Exception Type: DatabaseError
Exception Value:
no such table: django_admin_log
I tried running schemamigration myapp --auto but it's telling me nothing's changed. I even tried running convert_to_south django.contrib.admin, as well as trying a fake migration, but I keep getting the same error. Any help is appreciated.
second's answer is correct
only apps that make use of south will be managed by south
so using south isn't a possible solution.
Your issue is that the syncdb thinks that the table for django_admin_log has already been created.
Solution
open your django_content_type table.
delete the record for the name="log entry", app_label="admin", model="logentry" content type.
run syncdb again
only apps that make use of south will be managed by south
django.contrib.admin doesn't. instead you need another syncdb (it will leave any existing tables alone, so should be safe to run)