Django 2.0 + Postgres heroku migrations - python

I am trying to run a Django application with Postgres in Heroku. I have created an app for the application but the problem is that the app migrations are not being applied in Heroku. I am using the heroku starter base template and have committed the necessary migration files.
When I run
heroku run python manage.py migrate, the first set of migrations apply:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
blah blah blah
But when I run it the second time it gives me the the output
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
If I specify the app using
heroku run python manage.py migrate phone I get the error CommandError: No migrations present for: phone
In my local development, I have an app called phone that I have added to the settings.py and can run local migrations.

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 migrate --fake-initial reports relation already exist

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

Running migrations: No migrations to apply

I want to test django-embed-video from their official git reposiotry Django-embed-video example project.
Here are their instructions:
Example project For easy start with using django-embed-video, you can
take a look at example project. It is located in example_project
directory in root of repository.
Running example project Install requirements:
pip install -r requirements.txt
Create database:
python manage.py migrate --run-syncdb --noinput
Or, for older versions of Django:
python manage.py syncdb --noinput
Run testing server:
python manage.py runserver
Take a look at http://localhost:8000 . You can log in to
administration with username admin and password admin.
I followed all their instructions when I run.
python manage.py migrate --run-syncdb --noinput
I get the following
Operations to perform:
Synchronize unmigrated apps: embed_video, example_project, messages, posts
Apply all migrations: admin, auth, contenttypes, sessions, sites
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
No migrations to apply.
When I run
python manage.py runserver 8080
I get the following
Performing system checks...
System check identified no issues (0 silenced).
February 13, 2019 - 15:52:08
Django version 1.11.20, using settings 'example_project.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CTRL-BREAK.
When I go to http://127.0.0.1:8080/ I see the following
What am I doing wrong here?

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

Categories

Resources