Django: MySQL no such table: aidata.django_session - django

I'm running Django 1.4 on Windows 7 in Pycharm and I installed WAMP because I need to have my data in a MySQL table.
This is from setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aidata',
'USER': 'root'
}
}
From installed_apps I uncommented:
'django.contrib.sessions'
Running manage.py syncdb does not create any tables ( even models) in my mysqldb.
I get the error when trying to acces /admin/
DatabaseError at /admin/
(1146, "Table 'aidata.django_session' doesn't exist")

Double check the db credentials
make sure you uncommented this line in your middleware:
MIDDLEWARE_CLASSES = (
....
'django.contrib.sessions.middleware.SessionMiddleware',
)
then try to python manage.py syncdb.
if you are still having issues post any output
EDIT -- NEXT CHECK:
do you have a "django_content_type" table?
if so, does that table have a "session" record?
if so, delete the session record and try to python manage.py syncdb
EDIT -- STEP 3:
now i'm guessing, post up your settings file so i can make meaningful troubleshooting attempts
Stop your server if you have one running
go into your file browser and delete the settings.pyc file
try to python manage.py syncdb
my thought is that a pyc file with the sqlLite info may be cached and not regenerating
EDIT -- STEP 4:
everything in your settings.py look ok to me. try something for me? create a new django project, don't enable the admin or add in your apps i just want to know if from scratch everything in your django install seems to be working
django-admin.py startproject testsite
do the database configuration/setup
python manage.py syncdb
let me know if the models create properly

I was running into the same problem and for me (running django 1.7 development trunk of mid-sept.2013) it helped to
remove all south migrations ([app]/migration)-directories
remove south from INSTALLED_APPS in settings.py
That might be due to the shift towards the integrated migration system in django v1.7, but I'm speculating here.

Related

Heroku doesnt migrate models on Django

I just deploy my Django app to Heroku but I cant migrate my migrations to heroku. First I run :
heroku run python manage.py migrate all the migrations list as OK but when I showmigrations, none of them is migrating (all blank [ ]).
Then I try heroku run bash and migrate from there, everything seems ok even showmigrations from bash showing all of the migrations is working. I even manage to create a superuser. But when I open my admin page and log in with superuser it shows 'account.account' table does not exist and when I check showmigrations again all of the migrations are gone. I have been repeating this migration over and over and still can't figure this out.
Does anyone know what I am doing wrong here?
Edit :
I don't know if this is related but when I push my project to Heroku the first time, I'm using Postgre with this setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
but when I try to run migrations it shows an error something like cant access localhost with 5432 port, it turns out that Heroku trying to access postgre in my localhost instead of using Heroku Postgres. The solution I found is to dump my db and reload it to Heroku. And since I don't know how to set that up. I just comment that postgre setting and replace it with default django setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
create my dbsqlite file, running migrations again then once again push it to Heroku, hoping to find an easy way out but instead ended up in this problem
I'd recommend you to push a migrated project to heroku that is
python manage.py migrate
then push that migrated project to heroku
the reason your superuser doesn't get stored while creating superuser from heroku bash is because heroku has an ephemeral drive i.e heroku clears all the modified data from orignal push after equal interval of time.
It turns out that I forgot to install django-heroku. More on this : https://devcenter.heroku.com/articles/django-app-configuration

how to switch to a new database

I want to deploy my django project to the production environments, and associated it with an new empty database, and I did as follows :
Create an new empty database
Updated settings.py and pointed the database name to the new one
Deleted the migrations folder under my App
Run python manage.py runserver and no errors returned
Run python manage.py makemigrations and python manage.py migrate
but only auth related tables created ( like auth_user , auth_group ... ), no databases tables created for my Apps
How should I do for this situation to move to the new database for my project?
Deleted the migrations folder under my App
This was your mistake, you deleted the migrations - including the initial migrations. So when you go to makemigrations you haven't got the initial migration available.
So you need to run makemigrations <app_name> to at least get the initial migration.
If you were to do this again, don't delete the migrations, just change the database settings and then migrate.
Firstly, you should not have deleted the migrations. Now, make all the migrations again which you have deleted.
python manage.py makemigrations app_name
Do this for all the apps of which you have deleted the migrations.
Now, add your new database to settings.py. Do not remove the old one yet. For example, if I were adding a MySQL database, I would have added the following to the DATABASES dictionary in settings.py:
'new': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'databasename',
'USER': 'databaseusername',
'PASSWORD': 'databasepassword',
'HOST': 'localhost',
'PORT': '3306',
}
I have named the database as 'new'. Now we have two databases 'default' and 'new'. Now you have to create tables in the new database by running the migrations on the new database:
python manage.py migrate --database=new
You can follow these additional steps if you want to transfer your data to the new database. First, clear the new database:
python manage.py flush --database=new
Now export data from the old database into a json file:
python manage.py dumpdata>data.json
Import this data into the new database:
python manage.py loaddata data.json --database=new
Now you can remove the 'default' database and rename the 'new' database to 'default'.
The procedure mentioned in this answer is taken from my blog.
Just check the output of python manage.py makemigrations command, if it is showing no change detected then you need to check that have you added that app in your INSTALLED_APPS = [] in settings.py file or it might be the problem because you have deleted migration folder.Because if is there any database connectivity error it will show you that while doing makemigrations.
If your database has a new name, i.e. not "default", you need to specify it to migrate:
python manage.py migrate --database <newdb>

setting database: postgresql in django 1.8 in heroku

I have been struggling for this issue for the whole days while no solutions at all. so I post it here.
I am trying to set up a blog website in Heroku via Django 1.8 which uses Python 3.4.3. I follows the instructions from Heroku website here.
I use "foreman start" to run Django project in my Mac and I already installed all dependence.
Part of my setting.py file involving the database initially looks like:
import dj_database_url
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
Then I got error: ImproperlyConfigured at /settings.DATABASES is improperly configured. Please supply the ENGINE value.
Then I modify the files by adding one line supplying the ENGINE value:
import dj_database_url
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
Based on this post answered by Or Arbel, it should work. But I got another error: ImproperlyConfigured at /settings.DATABASES is improperly configured. Please supply the NAME value.
What should I do next? Actually my Django project is very simple and does not involve any database operations(may need in the future). I just want to make it works on Heroku. Thanks!
Do I need to create a database to continue? I just want to make the webpage works.
Thanks for your guys help, specially souldeux.
Update:
I have fixed the issue by using souldeux's method by providing more informations about the database. Here I want to emphasis that it seems the code from the original Heroku tutorial does not work for Django 1.8:
import dj_database_url ####not working for my case
DATABASES = {}
DATABASES['default'] = dj_database_url.config()
Initially I did not create a database because I think it is not necessary for simple projects, based on my understanding obtained from Heroku tutorial. Actually it does need to create a database in Heroku to make it works. The tutorial is here. You need run "heroku config -s | grep HEROKU_POSTGRESQL" to get the database information. The format is like:
scheme://username:password#host:port/database
So you can get 'database', 'username', 'password', etc.
Afterwards, modify the 'settings.py' according to souldeux, then run following codes:
git add .
git commit -m "Ready to go to Heroku"
git push heroku master
heroku run python manage.py syncdb
Now it works. But other issues arise like my webpages do not show images... Anyway it solved. Please confirm my solutions, thanks.
I think you need to add more information to your database definition. For instance, here's what my own database entry looks like in my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django',
'USER': 'redacted',
'PASSWORD': 'redacted',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
If you don't have a database user and password to enter in the fields marked redacted then you need to make sure you have the actual database created and psycopg2 installed.

Django 1.8, syncdb not working, throwing a foreign key constraint error

Since I upgrade to Django 1.8 from 1.7, I have got this foreign key constraint error.
File "c:project\env\lib\site-packages\mysql_python-1.2.5-py2.7-win32.egg/MySQLdb\connections.py line 36, in defaulterrorhandler raise errorclass, errorvalue,
Django.db.utils.IntergrityError: 'Cannot add foreing key contraint
What's some wrong with django 1.8 (latest version)?
Try this
DATABASES = {
'default': {
...
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
'STORAGE_ENGINE': 'MyISAM / INNODB / ETC'
}
}
Have you created migrations for all your apps? If not, you may well be hitting the problem that the database tables are being created in the wrong order, which will give you this error.
If you have an existing Django 1.7 project, then you need to create the initial migration files, and then fake the initial migration, as described here
https://docs.djangoproject.com/en/1.8/topics/migrations/#adding-migrations-to-apps
Create the migration with
$ python manage.py make migrations your_app_label
And then fake the application
$ python manage.py migrate --fake-initial your_app_label

django oracle inspectdb failure

i'm using django 1.3. i have an existing oracle database (10g) i would like to build Model's from using inspectdb.
'db': {
'ENGINE': 'django.db.backends.oracle',
'NAME': 'DBNAME',
'USER': 'me',
'PASSWORD': 'something',
}
so when run inspectdb i get:
$ python manage.py inspectdb --database db
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
so i add
$ export ORACLE_HOME=/usr/oracle/
$ TWO_TASK=DBNAME
i try logging on with sqlplus with the same credentials and everything looks good.
so... i run inspectdb again, but this time i get
# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field names.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'
# into your database.
from django.db import models
(ie it's blank)
any ideas? i had no problems getting this to work on a mysql database.
From the official docs.
inspectdb works with PostgreSQL, MySQL and SQLite. Foreign-key detection only works in PostgreSQL and with certain types of MySQL tables.
There is currently not a bug listed for it in the Django tracker if you wanted to submit it.
I have a similar setup at the top of my settings.py to set my environment variables for my oracle driver (Oracle 11.2). Not sure if this will help in your specific case.
### SETTING UP THE ENVIRONENT FOR OUR ORACLE RPM
import os
os.putenv('ORACLE_HOME', '/.../oracle/11.2')
os.putenv('LD_LIBRARY_PATH', '/.../oracle/11.2/lib')
I have had no issues with manage.py inspectdb (Django 1.2.7 and Django 1.4) on Oracle 11.2.