Local and heroku database out of sync (Django/Heroku) - django

I have been having challenges with my migrations from local to heroku. It appears that the schemas are out of sync.
Here is my local database schema from python manage.py showmigrations
account
[X] 0001_initial
[X] 0002_email_max_length
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
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
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
sites
[X] 0001_initial
[X] 0002_alter_domain_unique
socialaccount
[X] 0001_initial
[X] 0002_token_max_lengths
[X] 0003_extra_data_default_dict
testingland
[X] 0001_initial
Here is the heroku schemas from heroku run python manage.py showmigrations
account
[X] 0001_initial
[X] 0002_email_max_length
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
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
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
sites
[X] 0001_initial
[X] 0002_alter_domain_unique
socialaccount
[X] 0001_initial
[X] 0002_token_max_lengths
[X] 0003_extra_data_default_dict
I believe the problem stems from deleting the migrations folder in my testingland app earlier. I ran python3 manage.py makemigrations testingland and the migrations folder came back but when I run:
python manage.py makemigrations
git commit -a
git push heroku main
I just get Everything up-to-date and when I try to do anything in my heroku app I get this problem or something similar meaning the testingland app and schemas aren't available there:
ProgrammingError at /api/liked/
relation "testingland_liked" does not exist
LINE 1: ...er_id", "testingland_liked"."liked_venue_id" FROM "testingla...
Here are the installed apps in my settings.py file:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'testingland',
'rest_framework',
'bootstrap_modal_forms',
# all auth
# The following apps are required:
# 'django.contrib.auth',
# 'django.contrib.messages',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'widget_tweaks',
]
I have tried to be as clear as possible with this question - I am desperate for a solution.

Related

model doesn't have migrations

I am working on the Django model. I migrate all the files of the app, but it is showing me the model doesn't have migrations and also no migration file in the folder, but init.py file exists in the folder.
Please give me suggestions.
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
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
[X] 0011_update_proxy_permissions
[X] 0012_alter_user_first_name_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
First delete your all migrations folder and try below command:
python manage.py makemigrations appname
python manage.py sqlmigrate appname 0001 #This value will generate after makemigrations
python manage.py migrate

Django 3 error: No installed apps with label 'app_name'

I am currently learning how to use Django 3 (and Python 3.8) and I am following a tutorial to create a simple blog. I am getting a good grasp of the basic concepts so far but I am stuck at creating a migrations file.
When I enter the python manage.py makemigrations blog command into Windows Powershell, I get the error message: No installed app with label 'blog'. I have searched high and lo for an answer but I am still scratching my head. I am wondering if you lovely people would be able to offer any advice on the matter.
Here's my settings.py file:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print("BASE_DIR = ", BASE_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'django_project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'django_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
And here's my models.pyfile of the blog app:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100, unique=True)
email = models.EmailField(unique=True)
active = models.BooleanField(default=False)
created_on = models.DateTimeField()
last_logged_in = models.DateTimeFieldP()
class Category(models.Model):
name = model.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True
author = models.ForeignKey(Author, on_delete=models.CASCADE)
class Tag(model.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
class Post(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
content = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
tags = models.ManyToManyField(Tag)
I'll also post the directory to my blog app as well:
first_project
--my_venv
--django_project
----django_project
----__init__.py
----asgi.py
----settings.py
----urls.py
----wsgi.py
----blog
----migrations
----__init__.py
----templates
----__init__.py
----admin.py
----apps.py
----models.py
----tests.py
----urls.py
----views.py
----templates
----manage.py
----db.sqlite3
EDIT: Sorry about the missing blog app files, I've added the auto-generated files to the directory.
I'll also add the apps.py file, which is:
from django.apps import AppConfig
class BlogConfig(AppConfig):
name = 'blog'
As requested, this is the result of the command python manage.py showmigrations:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
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
[X] 0011_update_proxy_permissions
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
Thank you guys for all your help. I found the solution to this problem!
It was giving me the error as explained in my original post because of anti-virus software: Comodo. Once I temporarily deactivated that software and when I corrected all the silly mistakes in my models.py folder, the makemigrations process worked and I can carry on programming.
I just want to thank you guys again for taking the time to advise me on what to do.
P.S. I just wanted to add that this Comodo antivirus software, has caused all sorts of unexplained errors during my time learning Python and then Django, which was fixed once I temporarily deactivated the software
You have not created the blog app using the django way.
You should create an app in django using the following command.
python manage.py startapp blog
It looks like you don't have all the files like suggested in comments. At least you will need to have file apps.py under your app folder, but as you have models defined and you mentions models.py and it is not listed in your file list it seems that list is not complete.
File apps.py looks like this:
from django.apps import AppConfig
class BlogConfig(AppConfig):
name = 'blog'
Please check and post your complete file list.
Please Use this command to create app in django:
python manage.py startapp your_app_name
Or:
django-admin startapp your_app_name

Where is the Django migrations folder?

I must be doing something wrong. Everywhere I see people saying "Look at the migrations folder" but even though I can see migrations there is no folder.
Karls-Mac-mini:django_test karl$ tree
├── django_test
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── settings.cpython-35.pyc
│   │   └── urls.cpython-35.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
Karls-Mac-mini:django_test karl$ python manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
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
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
sessions
[X] 0001_initial
Short answer: the migrations originate from Django apps and third party apps you installed in INSTALLED_APPS. Not the ones you defined yourself.
Migrations are generated per app, and are stored in some_app/migrations.
Even if you do not define migrations for your apps, there will usually be migrations that take place, since you (likely) included some apps defined by Django (and other third parties) in your INSTALLED_APPS, these have migrations as well.
For example most likely your INSTALLED_APPS contains:
# settings.py
INSTALLED_APPS = [
# ...
'django.contrib.auth',
# ...
]
If we take a look at the source code of this app [GitHub], we see the migrations directiory. By adding this app to the INSTALLED_APPS you thus have added apps defined in the Django library itself (or by third parties) to your project, and the migrations of these apps are thus processed in the same way (in fact there is nothing "magical" about these apps, it is more that these handle common problems such that you do not need to care about these anymore).
The django.contrib.auth app has a file structure like (ommitting noise):
django/
contrib/
auth/
migrations/
__init__.py
0001_initial.py
0002_alter_permission_name_max_length.py
0003_alter_user_email_max_length.py
0004_alter_user_username_opts.py
0005_alter_user_last_login_null.py
0006_require_contenttypes_0002.py
0007_alter_validators_add_error_messages.py
0008_alter_user_username_max_length.py
0009_alter_user_last_name_max_length.py
These are extactly the same migrations you see on the console when you perform migrations for the auth app (second section).
Django Project is actually a combination of a few Applications and Configuration files.
Applications and Configuration are actually Python Modules/Packages. Every project has a few default apps installed and they are mentioned in INSTALLED_APPS (see project settings.py).
These are the default apps and they are not stored/installed in your project: they reside inside Django package by default.
Example:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
For example, django.contrib.admin app is used with your application: the migrations directory for this app is available at site-packages/django/contrib/admin/migrations, but migrations from apps that you created, are all stored inside each app folder.

Running django auth migrations doesn't create tables in db

I have been using django for many years but have not seen such issue before.
I am working on an old/legacy project. And I am originally facing this issue. So as the solution suggest, I run auth migrations first,
$python manage.py migrate auth
Operations to perform:
Apply all migrations: auth
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.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 auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
But when I check my mysql database, only django_migrations table gets created. I don't see auth_user, auth_group, auth_permissions, etc.
$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
mysql> show tables;
django_migrations
I am using Python3.6 and Django==1.11.5. My INSTALLED_APPS looks like this,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'debug_toolbar',
'djcelery',
'widget_tweaks',
'djangosaml2',
# ... more apps from my project
]
What could be wrong? Has anyone come across such issue.

Django not creating table for custom user

I've been trying to create a custom user to store extra fields in Django, but after specifying the new User and deleting the old database, Django does not want to make a table or any migrations for my app "accounts"
Error (when doing anything user related e.g. logging in):
django.db.utils.OperationalError: no such table: accounts_user
Auth User Model in settings.py:
AUTH_USER_MODEL = 'accounts.User'
Installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'forum',
]
accounts/models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
Edit:
I apologize, I forgot to mention. I have run makemigrations and migrate.
Makemigrations returns "No changes detected." Migrate does everything but any models existing in my accounts/models.py
Turns out I had to specifically run "makemigrations accounts". Not sure why, but it worked.