Django Simple Captcha: Table not found - django

On my local development machine I've already successfully installed django-simple-captcha. However, when I run it on the production server, the table is not found, resulting in a TemplateSyntaxError.
Caught DatabaseError while rendering: no such table: captcha_captchastore
I'm using sqlite3 as database engine and had already run python manage.py syncdb and checked the created tables with the following SQL statement:
sqlite> select name from sqlite_master where type = 'table';
auth_permission
auth_group_permissions
auth_group
auth_user_user_permissions
auth_user_groups
auth_user
auth_message
django_content_type
django_session
django_site
django_admin_log
captcha_captchastore
However, even if all the other tables work just fine, captcha_captchastore is not found. What could be possible reasons for this failure and how to fix it?
Additional findings:
When I run the Django development server on the production machine and check back everything works fine. Apparently it has something to do with the way I run Django. I currently have a setup with nginx and Django via fastcgi. The launch command is the following:
python manage.py runfcgi host=127.0.0.1 port=8081 --settings=settings
How can this influence finding the database or not? Probably there is a path problem.

I couldn't find out how the path to databases is constructed when you are in a template environment or in the scope of another app, but setting an absolute path name fixes the issue.

Related

Heroku Django postgres migration merge: psycopg2.errors.DuplicateTable: relation already exists

This is a Heroku-specific issue with a Django project 1.11.24 running Python 3.6.5 and a Heroku postgres database.
During testing of two different branches during development, different conflicting migration files were deployed at different times to the Heroku server. We recognized this, and have now merged the migrations, but the order the Heroku psql db schema was migrated is out of order with the current migration files.
As a result, specific tables already exist, so on deploy applying the updated merged migration files errs with:
psycopg2.errors.DuplicateTable: relation "table_foo" already exists
In heroku run python manage.py showmigrations -a appname all of the migrations are shown as having run.
We've followed Heroku's docs and done the following:
Rolled back the app itself to before when the conflicting migrations took place and were run (https://blog.heroku.com/releases-and-rollbacks)
Rolled back the postgres db itself to a datetime before when the conflicting migrations took place and were run (https://devcenter.heroku.com/articles/heroku-postgres-rollback)
However, despite both app and db rollbacks, when we check the db tables themselves in the rollback in pql shell with \dt, the table causing the DuplicateTable err still exists, so the db rollback doesn't actually seem to effect the django_migrations table.
It's Heroku, so we can't fake the migrations.
We could attempt to drop the specific db tables that already exist (or drop the entire db, since it's a test server), but that seems like bad practice. Is there any other way to address this in Heroku? thanks
I eventually fixed this by manually modifying migration files to align with the schema dependency order that was established. Very unsatisfying fix, wish Heroku offered a better solution for this (or a longer postgres database rollback window)

Restoring Django dump

I've been running a daily dump of a production Django application as follows:
./manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission -e sessions -e admin --all > data.json
Normally, restoring this to another installation for development hasn't caused a problem, but recently attempts to restore the data have caused this:
./manage.py loaddata -i data.json
django.db.utils.IntegrityError: Problem installing fixtures: The row in table 'reversion_version' with primary key '1' has an invalid foreign key: reversion_version.content_type_id contains a value '14' that does not have a corresponding value in django_content_type.id.
This suggests to me that the problem has been caused by the recent addition of django-reversion to the codebase, but I am not sure why and I have not been able to find any means of importing the backup. Some posts suggest that using natural keys may work, but then I get errors like:
django.core.serializers.base.DeserializationError: Problem installing fixture 'data.json': [u"'maintainer' value must be an integer."]
"maintainer" is in this case a reference to this bit of code in a model definition in models.py:
maintainer = models.ForeignKey(Organization,related_name="maintainer",blank=True,null=True)
Does anyone has any suggestions as to how I might get this dump installed, or modify the dump procedure to make a reproducible dump?
I note that the production site is using Postgres and the test site has SQLite, but this has never been a problem before.
On your local machine clone your project and do something like this:
Checkout the project at state that was used to create the dump.
Create a new database and tables.
Load the dump.
Update the code to current state.
Run migrations.
That was rather painful. It seems that the way to fix it was to dump django_content_types as csv from the production posgres database, delete the IDs from the resulting csv file, then do the following on the SQLite database for the test version:
CREATE TABLE temp_table(a, b, c)
.mode csv
.import content_type.csv temp_table
DELETE FROM sqlite_sequence WHERE name = 'django_content_type'
DELETE FROM django_content_type
INSERT INTO django_content_type(name,app_label,model) SELECT * FROM temp_table
That had the effect of setting the ids of the entries in the django_content_type table to match those in the dump, allowing the restore to proceed.

Django: Using south on project with existing migration files

This is a very basic question, but the other SO questions I read do not seam to answer it.
I checked out a project from a repository which contains some models some of them having migrations files.
I did not create any local database yet
What is the correct way of creating all the tables for the project?
I thought I start by executing
./manage.py syncdb
this creates some tables but for the once using south it tells me to run migrate. So I execute
./manage.py migrate
but then I get the error that
DatabaseError: table "mytable" already exists
I usually use ./manage.py syncdb --migrate and it does not throw that kind of error.
Are you sure that none of you other apps use a table with the same name?
You can checkout which tables your app wants to create by opening a django shell and doing sth like:
./manage.py sql YourAppName

South not Migrating django.contrib.admin

I have a Django project that's managed by South. I ran through the normal procedure when I first started the project:
Install South
Add South to INSTALLED_APPS
Perform initial syncdb
schemamigration --initial, followed by migrate myapp
Everything's been working fine and I've done several migrations since. Now I enabled the django admin portion of the site (INSTALLED_APPS, urls, etc.). When I tried adding a test user, I was received the following error:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/auth/user/add/
Django Version: 1.4.3
Exception Type: DatabaseError
Exception Value:
no such table: django_admin_log
I tried running schemamigration myapp --auto but it's telling me nothing's changed. I even tried running convert_to_south django.contrib.admin, as well as trying a fake migration, but I keep getting the same error. Any help is appreciated.
second's answer is correct
only apps that make use of south will be managed by south
so using south isn't a possible solution.
Your issue is that the syncdb thinks that the table for django_admin_log has already been created.
Solution
open your django_content_type table.
delete the record for the name="log entry", app_label="admin", model="logentry" content type.
run syncdb again
only apps that make use of south will be managed by south
django.contrib.admin doesn't. instead you need another syncdb (it will leave any existing tables alone, so should be safe to run)

newbie difficulty using south with pycharm - DatabaseError: no such table: south_migrationhistory

I'm using sqlite3 and pycharm to learn more about django, and googled to find that south is recommended to make it easier to modify models after they have been created.
I'm trying to follow the advice on http://south.aeracode.org/docs/tutorial/part1.html#starting-off.
The most success I've had so far is to create a simple model and run syncdb before adding south to installed_apps. That way the intial tables are created and I get a chance to create a super user. (Django admin seems to fret if there are no users).
Then I add south to installed_apps, and run django_manage.py schemamigration bookmarks --initial
It seems to work fine. A new directory is created called migrations with a couple of files in it in my app folder and an encouraging message.
"Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate bookmarks"
The next step - django_manage.py" migrate bookmarks generates the following error message
django.db.utils.DatabaseError: no such table: south_migrationhistory.
I thought that table would be created in the first schememigration step. What am I missing? Can anyone help?
Marg
South uses a table if its own to keep track of which migrations have been applied. Before you can apply any migrations, this must have been created, using python ./manage.py syncdb.
As well as for setting up south, you will find syncdb sometimes necessary for non-south apps in your project, such as the very common django.contrib.auth.
Note that as a convenience, you can run both in one go like this
python ./manage.py syncdb --migrate
My latest (unsuccessful) effort was the following
Create application – synch db – superuser created
Test run –admin screen shows basic tables
Add south, and syncdb from command line with manage.py syncdb – south_migrationhistory table created. Add basic vanilla model
Tried various combinations of manage.py syncdb –manage, and
schemamigration from Pycharm (if run from within pycharm a
migrations directory is created within the app
– if run from the command line the directory does not seem to be
created.)
Django admin screen shows table – but if I try to edit
the table it says that it doesn’t exist
Check database structure
using SQLite browser - table for newly created model doesn’t exist
I’m starting to think that the whole thing is not worth the time wasting hassle – maybe I’m better off just modifying the tables in SQLite browser
Answer in the similar question:
Run syncdb to add the Django and South tables to the database.