"no such table" error on Heroku after django syncdb passed - python

I'm trying to deploy my Django application to Heroku. The migrations are in my local Git. When I try:
git push heroku master
heroku run python manage.py syncdb
It applies the migrations and also promts me to create superuser, which I successfully do. Now the application is up and running, however when I try to log into the Django admin it's throwing:
OperationalError no such table: user_user
When I try
heroku run python manage.py makemigrations
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
It applies all migrations, but fails to create superuser throwing:
django.db.utils.OperationalError: no such table: user_user
Either way I can not have my database set up and migrated on Heroku.
My database settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
My user model is:
class User(AbstractUser):
rating = models.PositiveIntegerField(default=settings.DEFAULT_USER_RATING)
Django version is 1.7.1.
How do I get my database tables created on Heroku?

You must not use sqlite3 on Heroku.
sqlite stores the database as a file on disk. But the filesystem in a Heroku dyno is not persistent, and is not shared between dynos. So, when you do heroku run python manage.py migrate, Heroku spins up a new dyno with a blank database, runs the migrations, then deletes the dyno and the database. The dyno that's running your site is unaffected, and never gets migrated.
You must use one of the Heroku database add-ons. There is a free tier for Postgres. You should use the dj-database-url library to set your database settings dynamically from the environment variables which Heroku sets.
Also, for the same reason, you must do manage.py makemigrations locally, commit the result to git, then push to Heroku.

You can use postgresql:
In settings.py add(at the end of file):
# ie if Heroku server
if 'DATABASE_URL' in os.environ:
import dj_database_url
DATABASES = {'default': dj_database_url.config()}
In requirements.txt add:
dj-database-url
psycopg2
Now you can run:
heroku run python manage.py migrate

pip install django-heroku
Add import django_heroku at top of file settings.py
Place django_heroku.settings(locals()) at the very bottom of settings.py
It will automatically configure your db. You can learn more on their website

What version of django you are using..?
If you are using django>=1.7 you need to run migrate
After adding models you need to do
python manage.py makemigrations then python manage.py migrate
If your project already contain migrations you can directly run python manage.py migrate command.
If you miss any step mentioned above please do that.

Related

Heroku No migrations to apply

I'm having trouble with my database at Heroku:
I've added some models for my App and whenever I run makemigrations it detects those changes. However when I run migrate it only says no migrations to apply. Please help!
I'VE FINALLY FIXED IT!!! This is what I did:
First I did a backup Download by heroku pg:backups:restore capture
then heroku pg:backups:restore download
it made a "latest.dump" file
then I used heroku pg:reset
it deleted the recent database of host
then I applied my fixes on my models and forms
new fields has to have "defaults=None" for models with Data you want to preserve (If you won't do this, the merge for this model won't take effect and will be cancelled)
then I run heroku run bash
run python manage.py makemigrations appname
then migrate
and then used "pg_restore --verbose --clean --no-acl --no-owner -h yourDBhost -U yourDBuser -d yourDBname latest.dump" (Your database credentials will be found on your postgresql addon settings) and then type or copy-paste yourDBpassword(from credentials also) to merge the backed up database with the new one with changes
and know it's done and perfectly working!

Django-constance: relation "constance_config" does not exist

I am having trouble using django-constance.
I've followed the steps here: https://django-constance.readthedocs.io/en/latest/index.html:
pip install "django-constance[database]"
add 'constance' and 'constance.backends.database', to INSTALLED_APPS
placed the following at the bottom of the settings file (it isn't callled setings.py but common.py):
CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend'
CONSTANCE_DATABASE_PREFIX = 'constance:my_app_name:'
CONSTANCE_CONFIG = {
'THE_ANSWER': (42,
'Answer to the Ultimate Question of Life, The Universe, and Everything'),
}
then ran python manage.py migrate database
But a table for dynamic settings wasn't created. This what happpens when I try to list constance settings:
$ python manage.py constance list
...
django.db.utils.ProgrammingError: relation "constance_config" does not exist
LINE 1: ...ce_config"."key", "constance_config"."value" FROM "constance...
I am running Python 3.5.2, Django 1.11.3 and django-constance 2.0.0.
Any clue what is going on?
I am not exactly sure why, but here's what worked:
the database initial migration was present
$ python manage.py showmigrations database
database
[X] 0001_initial
but the table itself was not
\dt *constance*
No matching relations found.
so I've removed that migration from django_migrations
delete from django_migrations where app = 'database';
re-ran the migration
python manage.py migrate database
and that's it. constance list behaves:
$ python manage.py constance list
THE_ANSWER 42
Check INSTALLED_APPS on the your project settings file, if you want using database backend change similar below:
INSTALLED_APPS = (
# other apps
'constance.backends.database',
)
Read more

Django on Heroku: relation does not exist

I built a Django 1.9 project locally with sqlite3 as my default database. I have an application named Download which defines the DownloadedSongs table in models.py:
models.py
from __future__ import unicode_literals
from django.db import models
class DownloadedSongs(models.Model):
song_name = models.CharField(max_length = 255)
song_artist = models.CharField(max_length = 255)
def __str__(self):
return self.song_name + ' - ' + self.song_artist
Now, in order to deploy my local project to Heroku, I added the following lines at the bottom of my settings.py file:
import dj_database_url
DATABASES['default'] = dj_database_url.config()
My application has a form with a couple of text fields, and on submitting that form, the data gets inserted into the DownloadedSongs table. Now, when I deployed my project on Heroku and tried submitting this form, I got the following error:
Exception Type: ProgrammingError at /download/
Exception Value: relation "Download_downloadedsongs" does not exist
LINE 1: INSERT INTO "Download_downloadedsongs" ("song_name", "song_a...
This is how my requirements.txt file looks like:
beautifulsoup4==4.4.1
cssselect==0.9.1
dj-database-url==0.4.1
dj-static==0.0.6
Django==1.9
django-toolbelt==0.0.1
gunicorn==19.6.0
lxml==3.6.0
psycopg2==2.6.1
requests==2.10.0
static3==0.7.0
Also, I did try to run the following commands as well:
heroku run python manage.py makemigrations
heroku run python manage.py migrate
However, the issue still persists. What seems to be wrong here?
Make sure your local migration folder and content is under git version control.
If not, add, commit & push them as follows (assuming you have a migrations folder under <myapp>, and your git remote is called 'heroku'):
git add <myapp>/migrations/*
git commit -m "Fix Heroku deployment"
git push heroku
Wait until the push is successful and you get the local prompt back.
Then log in to heroku and there execute migrate.
To do this in one execution environment, do not launch these as individual heroku commands, but launch a bash shell and execute both commands in there: (do not type the '~$', this represents the Heroku prompt)
heroku run bash
~$ ./manage.py migrate
~$ exit
You must not run makemigrations via heroku run. You must run it locally, and commit the result to git. Then you can deploy that code and run those generated migrations via heroku run python manage.py migrate.
The reason is that heroku run spins up a new dyno each time, with a new filesystem, so any migrations generated in the first command are lost by the time the second command runs. But in any case, migrations are part of your code, and must be in version control.
As Heroku's dynos don't have a filesystem that persists across deploys, a file-based database like SQLite3 isn't going to be suitable. It's a great DB for development/quick prototypes, though. https://stackoverflow.com/a/31395988/784648
So between deploys your entire SQLite database is going to be wiped, you should move onto a dedicated database when you deploy to heroku I think. I know heroku has a free tier for postgres databases which I'd recommend if you just want to test deployment to heroku.
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
this worked for me.
I know this is old, but I had this issue and found this thread useful.
To sum up, the error can also appear when executing the migrations (which is supposed to create the needed relations in the DB), because recent versions of Django check your urls.py before doing the migrations. In my case - and in many others' it seems, loading urls.py meant loading the views, and some views were class-based and had an attribute defined through get_object_or_404:
class CustomView(ParentCustomView):
phase = get_object_or_404(Phase, code='C')
This is what was evaluated before the migrations actually ran, and caused the error. I fixed it by turning my view's attribute as a property:
class CustomView(ParentCustomView):
#property
def phase(self):
return get_object_or_404(Phase, code='C')
You'll know quite easily if this is the problem you are encountering, as the Traceback will point you towards the problematic view.
Also this problem might not appear in development because you have migrated before creating the view.

How to apply a particular django database migration in heroku?

I uploaded my modified code with some changes in models. When I run heroku run python manage.py migrate app to apply the database migrations it gave me an error
CommandError: Conflicting migrations detected (0004_auto_20150819_0827, 0008_auto_20150813_1444 in app).
To fix them run 'python manage.py makemigrations --merge'
So when I run heroku run python manage.py makemigrations --merge it gave me output:
Created new merge migration /app/app/migrations/0009_merge.py
Now how can apply this migration to my database ?
Maybe
heroku run python manage.py migrate
or to see what it's going to apply:
heroku run python manage.py showmigrations

South 'nothing to migrate' message after successful schema migration

I have a Django app that I added South to, performed some migrations with, and runs as expected on my local machine. However, I have had nothing but database errors after pushing my project to Heroku.
In trying to deal with one database error I am experiencing, I attempted a test where I deleted one of my models, pushed the edited models file to Heroku and ran:
heroku run python manage.py schemamigration django_app test_remove_pub --auto
This seemed to work fine. I got back the message:
Running `python manage.py schemamigration apricot_app test_remove_pub --auto` attached
to terminal... up, run.6408
- Deleted model django_app.Publication
- Deleted M2M table for journalists on django_app.Publication
- Deleted M2M table for tags on apricot_app.Publication
Created 0006_test_remove_pub.py. You can now apply this migration with: ./manage.py
migrate django_app
So, South appeared to do everything I expected - it deleted my model and its many to many relationships and made the appropriate migration file. Next, I enter:
heroku run python manage.py migrate django_app
And I get back:
Running `python manage.py migrate django_app` attached to terminal... up, run.4792
Running migrations for django_app:
- Nothing to migrate.
- Loading initial data for django_app.
Installed 0 object(s) from 0 fixture(s)
Why would it say "nothing to migrate" when obviously there are things to migrate??
After you change the models, you need to do :
heroku run python manage.py schemamigration django_app --auto
if anything has changed
and then run
heroku run python manage.py migrate django_app
South applies migrations based on the entries in this database table: south_migrationhistory. So if you want to manually override it,
Remove all the entries with column app_name of the models you changed
Manually remove all the related tables. You can get a list of all the tables by typing this in the django shell:
from django.db.models import get_app, get_models
app = get_app(app_name)
for model in get_models(app, include_auto_created=True):
print model._meta.db_table
Remove the migrations/ folder related to the app
Do a fresh migration: ./manage.py schemamigration app_name --initial
Apply the migration ./manage.py migrate app_name

Categories