I have everything set up in my local environment and the code for my website up on the Heroku server, I'm just having serious trouble getting the schema to migrate to the PostgreSQL database on the Heroku server. Whenever I attempt heroku run python manage.py migrate I get the following (this would be an initial migration):
Running python manage.py migrate on baseballstatview... up, run.1653 (Free)
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, statview
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
Applying statview.0001_initial... OK
Which seems fine but then using heroku pg:info it tells me I have 0 tables, and even further when I run heroku run python manage.py showmigrations this is what I get:
Running python manage.py showmigrations on baseballstatview... up, run.5059 (Free)
admin
[ ] 0001_initial
[ ] 0002_logentry_remove_auto_add
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
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
sessions
[ ] 0001_initial
statview
[ ] 0001_initial
So it appears that the migrations are not going through and I'm wondering why that's the case. The database is empty, I've tried resetting it and trying again but nothing seems to work.
EDIT: below is the relevant settings.pyfor dj_database_url:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mlb_data',
'USER': 'postgres',
'PASSWORD': 'pw',
'HOST': 'localhost',
'PORT': '5432',
}
}
import dj_database_url
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
DEBUG = False
try:
from .local_settings import *
except ImportError:
pass
It looks from your code snippet that you are loading your dj_database_url configs BEFORE you attempt to bring in your local settings. This is the problem. What's happening here is that your local settings are overriding the production settings (with Postgres).
What's happening when you run the migration is:
Heroku is creating a new local SQLite database.
Doing the migrations.
Saying it's successful.
Exiting.
If you move that local settings stuff up above your db_database_url call, things should start magically working for ya!
Related
I aim to use postgres as default database for my django models. I am using docker-compose for the postgres and it seems to be up and running.
version: '3.2'
services:
postgres:
image: postgres:13.4
environment:
POSTGRES_DB: backend_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- database-data:/var/lib/postgresql/data/
ports:
- 5432:5432
networks:
- postgres
volumes:
database-data:
driver: local
networks:
postgres:
driver: bridge
However, when I am doing python3 manage.py migrate --database postgres I am getting the following:
Operations to perform:
Apply all migrations: admin, auth, authentication, authtoken, contenttypes, sessions, token_blacklist, vpp_optimization
Running migrations:
No migrations to apply.
The problem is evident also when I am doing python3 manage.py runserver, where I am getting:
Performing system checks...
System check identified no issues (0 silenced).
You have 31 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, authentication, authtoken, contenttypes, sessions, token_blacklist, vpp_optimization.
Run 'python manage.py migrate' to apply them.
April 11, 2022 - 05:32:10
Django version 3.1.7, using settings 'core.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
It appears like the command python3 manage.py migrate --database postgres was not executed.
Here is the settings.py part of the databases:
DATABASES = {
'default': get_config(
'DATABASE_URL',
'sqlite:///' + BASE_DIR.child('db.sqlite3'),
cast=db_url
),
'postgres': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': get_config('DB_NAME', 'backend_db'),
'USER': get_config('DB_USER', 'postgres'),
'PASSWORD': get_config('DB_PASSWORD', 'postgres'),
'HOST': get_config('DB_HOST', 'postgres-service'),
'PORT': get_config('DB_PORT', '5432')
}
}
Any idea of what I am doing wrong here?
The problem is that when you run:
python3 manage.py migrate --database postgres
You are explicitly defining that the database where you want to migrate the database schema is in postgres. But when you run the app you use:
python3 manage.py runserver
And according to your settings is there anything that indicate that you will use the postgres db. So I think at this point you are usign the sqlite db.
One thing that you can do is to setup an env variable to specify which database to use when running the app. Hope it can help.
So I'm working on hosting a project and on my Heroku dashboard it says it's live, but every time I try to access it I get a 500 Server Error, even when trying to access it locally via Heroku CLI.
I tried running migrations because I get the following warning:
You have 21 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, recipe, sessions, users.
Run 'python manage.py migrate' to apply them.
Even after running makemigrations and migrate, as can be seen in screenshot below, any suggestions to what I might be missing?
C:\Users\davba\OneDrive\Documents\David\CSC-456\LIA_Project\Leftover-Ingredients->heroku run -a fathomless-cliffs-95117 python manage.py makemigrations
Running python manage.py makemigrations on ⬢ fathomless-cliffs-95117... up, run.3466 (Free)
No changes detected
C:\Users\davba\OneDrive\Documents\David\CSC-456\LIA_Project\Leftover-Ingredients->heroku run -a fathomless-cliffs-95117 python manage.py migrate
Running python manage.py migrate on ⬢ fathomless-cliffs-95117... up, run.3409 (Free)
Operations to perform:
Apply all migrations: admin, auth, contenttypes, recipe, sessions, users
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 admin.0003_logentry_add_action_flag_choices... 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 auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying recipe.0001_initial... OK
Applying sessions.0001_initial... OK
Applying users.0001_initial... OK
Applying users.0002_alter_profile_image... OK
C:\Users\davba\OneDrive\Documents\David\CSC-456\LIA_Project\Leftover-Ingredients->heroku run -a fathomless-cliffs-95117 python manage.py runserver
Running python manage.py runserver on ⬢ fathomless-cliffs-95117... up, run.6946 (Free)
Performing system checks...
System check identified no issues (0 silenced).
You have 21 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, recipe, sessions, users.
Run 'python manage.py migrate' to apply them.
November 17, 2021 - 00:20:55
Django version 3.2.8, using settings 'LeftoverIngredients.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I'm having a weird problem when migrating my project from django 1.7.4 to 1.8.5
In my project I extend the basic User model like so:
App users:
class User(AbstractUser):
age = models.IntegerField()
def __unicode__(self):
return self.username
Now when migrating in django 1.8.5 for some reasons I have to start by doing
python manage.py makemigrations
Which will make the migrations for the users app.
If I do
python manage.py migrate
directly after this it fails with this error
django.db.utils.ProgrammingError: relation "users_user" does not exist
Then I do:
python manage.py migrate users
Which fails
"Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure
contenttypes is migrated before trying to migrate apps individually.
What is interesting is that even if this fails, now running
python manage.py migrate
Works
Operations to perform:
Synchronize unmigrated apps: messages, staticfiles, django_extensions, allauth, avatar, crispy_forms, debug_toolbar
Apply all migrations: sessions, users, contenttypes, admin, sites, account, auth, socialaccount
Synchronizing apps without migrations:
Creating tables...
Creating table avatar_avatar
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying account.0001_initial... OK
Applying account.0002_email_max_length... OK
Applying admin.0001_initial... 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 sessions.0001_initial... OK
Applying sites.0001_initial... OK
Applying sites.0002_set_site_domain_and_name... OK
Applying sites.0003_auto_20151104_1309... OK
Applying socialaccount.0001_initial... OK
Has anyone experienced the same problems when migrating from an older django version to 1.8?
contenttypes is migrated before trying to migrate apps individually.
All you need to do is add the dependencies so your migrations will run in order.
Have a look at django.contrib.contenttypes.migrations, add the latest one as a dependency in account.migrations and it should all work fine.
I just clone exiting project from github, and dump mysql database in my local machine. Every thing is working fine. I made some changes in myapp/model.py, like add new tables. After that when run
1. python ./manage.py makemigrations myapp. Then it makes migration files like
Migrations for 'myapp':
0001_initial.py:
- Create model AndroidRegkey
- Create model ApkVersion
.......................
.......................
python manage.py migrate myapp it gives following message
Operations to perform:
Apply all migrations: myapp
Running migrations:
No migrations to apply.
This is the first time migration on my local machine. I already have database. But after Adding new models in model.py first time it does not apply any migration to data base, why?
I also go this link stack-over flow but not working.
When I ran python manage.py migrate --list Getting following result.
admin
[X] 0001_initial
auth
[X] 0001_initial
contenttypes
[X] 0001_initial
intracity
(no migrations)
mailer
[X] 0001_initial
[X] 0002_auto_20150720_1433
sessions
[X] 0001_initial
When you apply migrations, the migrations that have been applied are stored in the database. They will also be exported to your database dump. So your database should already be in the correct state after you import the data locally. Django looks at the relevant table, sees you're up to date, and takes no further action.
I'm using django 1.8 in my project and it seems like the auth models are not being created. I have applied the auth migrations and I get;
python manage.py migrate --database=default auth
Operations to perform:
Apply all migrations: auth
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
But only the django_content_type table is created. Am also using Postgres.
Can anyone please give me pointers on how I can solve this.
Thank you.
edit:
running ;
python manage.py createsuper
part of error dump
django.db.utils.ProgrammingError: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
Edit
settings.py
....
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
........
DATABASES = {
'default': {
'NAME': 'dbname',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': 'theuser',
'PASSWORD': 'thepassword',
'HOST':'thehost'
},
'auth_db': {
'NAME': 'dbname',
'ENGINE': 'django.db.backends.mysql',
'USER': 'otheruser',
'PASSWORD': 'thepassword',
'HOST':'thehost'
}
}
....
Am using 2 database connections where the mysql database is used to authenticate existing users. The idea is to use already existing users on the mysql database on the new application.
Try migrating your auth app manually followed by default migrate command.
python manage.py migrate auth
python manage.py migrate
hope this helps.
I have solved the problem finally by removing the db routers. This is because in my application as described above, i'm routing the auth requests to another db where there are already existing.
Such that running
python manage.py sqlmigrate auth 000_initial
Produces and empty sql statement.
Removing the db routers from the settings config proceeded by running
python manage.py migrate auth
produces the 'sql statements' hence migration of downstream apps proceeds without a problem.