Django migrate --fake-initial reports relation already exist - python

I am trying to import an existing database into my Django project, so I run python manage.py migrate --fake-initial, but I get this error:
operations to perform:
Apply all migrations: ExcursionsManagerApp, GeneralApp, InvoicesManagerApp, OperationsManagerApp, PaymentsManagerApp, RatesMan
agerApp, ReportsManagerApp, ReservationsManagerApp, UsersManagerApp, admin, auth, authtoken, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... FAKED
Applying auth.0001_initial... FAKED
Applying contenttypes.0002_remove_content_type_name... OK
Applying GeneralApp.0001_initial...Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/IntellibookWebProject/IntellibookWebVenv/lib/python3.6/site-packages/django/db/back
ends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: relation "GeneralApp_airport" already exists
Of course all the tables already exist in the database, that is the reason why I use --fake-initial, that is supposed to fake the creation of database objects.
Why is migrate attempting to create the table GeneralApp__airport instead of faking it?

Just by running the following command would solve the problem:
python manage.py migrate --fake

--fake-initial can't deal with any situation where some of the tables listed in the initial migration exist and some do not. So, if tables exist for some, but not all, of the CreateModel()s in the operations list in your 0001_initial.py, --fake-initial does not apply and it tries to create tables for ALL of the models.
Does that make sense? Not to me...

Steps you can follow to make migrations from an existing database. Firstly empty the django migration table from the database.
delete from django_migrations
Remove migrations from your migrations folder for the app
rm -rf <app>/migrations/
Reset the migration for builtin apps(like admin)
python manage.py migrate --fake
Create initial migration for each and every app
python manage.py makemigrations <app>
The final step is to create fake initial migrations
python manage.py migrate --fake-initial

Related

' No migrations to apply' after Django migration

I made a new app called 'contact' which I added to installed_apps, but I can't create a table in the SQLite database!
Django version = 3.1
python manage.py makemigrations contact
No changes detected in app 'contact'
then for : python manage.py migrate contact
Operations to perform:
Apply all migrations: (none)
Running migrations:
No migrations to apply.
I have tried many solutions proposed by members here but None worked
like checking "init" file existed in migrations folder and it's empty or commands like these :
python manage.py migrate --fake contact zero
Operations to perform:
Unapply all migrations: contact
Running migrations:
No migrations to apply.
python manage.py migrate contact --fake-initial
Operations to perform:
Apply all migrations: (none)
Running migrations:
No migrations to apply.
This worked for me;
python manage.py makemigrations --empty yourappname
python manage.py migrate yourappname
It turns out adding an extra empty migration forces django to recheck the table and in the process, it noticed the new migrations. There's probably some caching taking place somewhere.
Original answer
Try not to use the name of your app.
Eg.
python manage.py makemigrations
python manage.py migrate
If don't work. You can do something more risky. Go to the directory of your project. Take the database to another folder. Make the same above commands.

Django: no migrations apply, but I have unapplied migrations

I deleted my database and tried to create a new one.
I did:
python manage.py makemigrations
python manage.py migrate
Then I ran python manage.py runserver and it returned
You have 2 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): social_django.
Run 'python manage.py migrate' to apply them.
Then I ran python manage.py migrate and it returned
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, social_django
Running migrations:
No migrations to apply.
I am using django 3.0.7 and social-auth-app-django 4.0.0
The solution was to disable the environment.
I don't know why.
You can check to see which migrations have been done by viewing your database with whichever program you use to do that. And checking the django_migrations table.
The migrations that need to be applied will still be in the migrations folders for the apps that haven't been updated.
To get them migrated, assuming they're still in your installed_apps in your settings.py, simply delete the migrations (probably __init__.py) from the migrations folders and re-run :
> python manage.py makemigrations
> python manage.py migrate
run the following command on your command prompt:
1.python manage.py migrate
2.python manage.py runserver

Django 2.2.4 - “No migrations to apply” when run migrate after makemigrations

I am trying to do a migration in django 2.2.4 in python 3.7.
First I try to make do makemigations:
python3 manage.py makemigrations
I get:
Migrations for 'main':
main/migrations/0001_initial.py
- Create model TutorialCategory
- Create model TutorialSeries
- Create model Tutorial
But then I try the second step:
python3 manage.py migrate
I get:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, main, sessions
Running migrations:
No migrations to apply.
Even though a migration should happen.
I tried deleting my migrations folder and then remaking it (with the empty __init__.py file inside) but it still doesn't work.
(Note: I have been following along the tutorial: Linking models with Foreign Keys - Django Web Development with Python p.9 by sentdex)
I faced the same problem in django 2.2, The following worked for me...
delete the migrations folder resided inside the app folder
delete the pycache folder too
restart the server [if your server is on and you are working from another cli]
python manage.py makemigrations <app_name> zero
python manage.py makemigrations <app_name> [explicit app_name is important]
python manage.py migrate
Somehow your migrations are virtually or faked applied in the database, Truncating django_migrations table should work.
Delete all the migrations files:
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete
Truncate table:
truncate django_migrations
makemigrations, migrate.
w/in the app directory I deleted the pycache and migrations folders,
from django.migrations tables I deleted all rows like this for PostgreSQL
DELETE FROM public.django_migrations
WHERE public.django_migrations.app = 'target_app_name';
To delete any app tables already created for the tables to be created from scratch.
Mine didn't migrate cause there was already a record in the django_migrations table with the same name, so I just removed it and then migrate worked.

Tables not showing up and migrations not visible after `python manage.py migrate`

I'm following the official tutorial on Django and have run python manage.py migrate in https://docs.djangoproject.com/en/1.11/intro/tutorial02/ . After this, I'd expect some files to show up in polls/migrations, but there is just an empty __init__.py there, and when I run sqlite3 and type .tables or .schema, nothing is output. Still, the python manage.py migrate command seems successful:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
What's going wrong here?
EDIT:
Added 'polls', to my INSTALLED_APPS. Then:
$ python manage.py makemigrations
Migrations for 'polls':
polls/migrations/0001_initial.py
- Create model Choice
- Create model Question
- Add field question to choice
(django) Sahands-MacBook-Pro:mysite sahandzarrinkoub$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying polls.0001_initial... OK
(django) Sahands-MacBook-Pro:mysite sahandzarrinkoub$ sqlite3
SQLite version 3.16.0 2016-11-04 19:09:39
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .schema
sqlite>
Same problem.
EDIT2: Upon running python manage.py dbshell, .schema and .tables finally produced output.
At first, check if your polls app exists in your INSTALLED_APPS list in settings.py:
INSTALLED_APPS = [
# ...
'polls',
]
And then try to run makemigrations before doing migrate:
$ python manage.py makemigrations
$ python3 manage.py migrate
EDIT:
Now, as your polls table is created in your database, you can access the sqlite3 client by running $ python manage.py dbshell:
$ python manage.py dbshell
sqlite> .schema
The difference between running just sqlite3 and python manage.py dbshell is said in the docs:
django-admin dbshell
Runs the command-line client for the database engine specified in your ENGINE setting, with the connection parameters specified in your USER, PASSWORD, etc., settings.
You might have done something wrong or you might not have. It depends
If you have not created any model in models.py file and you have registered you app in settings.py INSTALLED_APPS
In this case, you are not doing anything wrong. Django will create a migration in the migrations folder of your app when you have created at least one model and you have register your app in INSTALLED_APPS
If you have created models but have forgotten to add your app to INSTALLED_APPS
In this case, you just have to add your app's name to INSTALLED_APPS
Hope this helps

Django Heroku Error "Your models have changes that are not yet reflected in a migration"

I recently added a model to my app (UserProfile) and when I pushed the changes to Heroku, I think I accidentally ran heroku run python manage.py makemigrations. Now when I try to run heroku run python manage.py migrate I get the error below
(leaguemaster) benjamins-mbp-2:leaguemaster Ben$ heroku run python manage.py migrate
Running `python manage.py migrate` attached to terminal... up, run.1357
Operations to perform:
Synchronize unmigrated apps: allauth
Apply all migrations: auth, admin, socialaccount, sites, accounts, account, contenttypes, sessions, leagueapp
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
No migrations to apply.
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
How do I fix this? Please help!
You need to first create the migrations locally, add them to your repository, commit the files with the new migrations and then push to heroku.
The sequence is something like this:
1. (add/modify some someapp/models.py)
2. python manage.py makemigrations someapp
3. python manage.py migrate
4. git add someapp/migrations/*.py (to add the new migration file)
5. git commit -m "added migration for app someapp"
6. git push heroku
7. heroku run python manage.py migrate
1. Make migrations locally
$ python manage.py makemigrations && python manage.py migrate
2. Commit changes and push it on the server
$ git add --all
$ git commit -m "Fixed migrate error"
$ git push heroku master
3. Now run migrate on the server
$ heroku run python manage.py migrate
You also need to be sure that you haven't ignored these migration paths in your .gitingnore file
It sounds like you ran makemigrations after you made changes to your model but before you had an initial migration file. Try to revert your app to the state it was before you added the new model and run makemigrations again to create the initial migration. Then add your updates back in and run makemigrations once more. This will create a second migration from your initial data structure to the new updated one. Then try your deployment.
https://docs.djangoproject.com/en/1.7/topics/migrations/#adding-migrations-to-apps
Answered for my case:
your_field=models.CharField(max_length=9,default=False)
Convert to
your_field=models.CharField(max_length=9,default='False')
My case:
In models.py for the field I wanted to set the default value to False. First default = False my contract without ''. But after running python manage.py migrate, I got the above error. The problem was solved after placing False inside ''.
Sometimes it is necessary for the default value in our field model to be a False string type.
If
default = False
If you write in the model, you will encounter this error.
In fact, depending on the type of field we have, we can not always set the default value of a field in the model to True or False. Must be converted to string for CharField type.

Categories

Resources