production issue: sqlite3 to postgresql migration issue in django - django

I am trying to migrate my django project from sqlite3 to postgresql. I have done this succesfully in development but getting errors in production.
i have used this command to create dumps:
manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
then i changed database in my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
then i exclude contenttypes as follow:
python3 manage.py shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
quit()
but i am getting this error:
django.db.utils.IntegrityError: Problem installing fixtures: insert or update on table
"django_admin_log" violates foreign key constraint
"django_admin_log_content_type_id_c4bce8eb_fk_django_co"
DETAIL: Key (content_type_id)=(7) is not present in table "django_content_type".
please help

okay i figured out and migrated succesfully.
thanks to ankit, Willem Van Onsem and Mostafa Ghadimi
I followed Mostafa Ghadimi's answer here step by step.
also cleared cache suggested byankit

Related

How to migrate the second db in django

I have two databases, one is default and the other one as first what I want is made some changes in first_db and now I want to migrate.
In my settings.py:
DATABASE_ROUTERS = ('app.router.DatabaseRouter',)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'primary_name',
'USER': 'root',
'PASSWORD': 'password',
},
'first': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'secondary_name',
'USER': 'root',
'PASSWORD': 'password',
}
}
I tried the command python manage.py migrate --database=first it showed it got migrated then when i tried to do python manage.py makemigrations it said all migrations are done and when i tried to run server and trying to insert some value in some table of first it started giving me error like the column I added is not there can anyone help me.
I think the migration didn't happen actually so anyone can show me how correctly migrate when having multiple database.
Migrations are done in database named 'first'.
if you want to migrate them to default database too just in case, then run:
python3 manage.py migrate
otherwise run:
python3 manage.py migrate --fake #To fake migration in default db for resolving runtime error
And to insert some value into table:
ModelName.objects.using('first').create(id=1 .......(other_datas)
Reference

django-admin dbshell raises django.core.exceptions.ImproperlyConfigured

I know this question has been asked before, but none of them worked for me so far, so I'm going to give it a chance here.
I'm trying to use MySQL as my database in django, but when I modify the settings.py and run the command:
django-admin dbshell
I get the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
What I did:
- I'm running windows 10.
using pipenv, I create fresh virtual environment.
install django.
start new project.
edit the settings.py
in the settings.py I change the DATABASES to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
also pip installed mysqlclient
Weird thing is that when I make migrations, new tables are created, which means that DB works. why doesn't the command
django-admin dbshell
work?
based on django docs, manage.py does same thing as django-admin, and also adds settings to the sys path, therefore manage.py should be used instead of django-admin
https://docs.djangoproject.com/en/2.1/ref/django-admin/

Mezzanine: where is the database created by `manage.py createdb`

I have the following database settings in Mezzanine:
DATABASES = {
"default": {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lucidDB',
'USER': 'lucid',
'PASSWORD': 'xxxxxxxx',
'HOST': 'localhost',
'PORT': '',
}
}
I run the command python manage.py createdb and then answer yes to the question Would you like to fake initial migrations? (yes/no):.
Note: south was installed.
My questions:
I checked in postgres (postgres# psql --> postgres=# \l), but didn't find the database lucidDB. However, the system runs OK. Where exactly is the database created?
What does it mean by fake migration?
I didn't run syncdb or makemigrations --> migrate yet, why the system worked?
You didn't find the LucidDB in postgres because it wasn't created, you need to create it with the postgres shell see here for detailed description.
Faking a migration is marking it as complete without actually changing the database schema, it will simply add an entry in the migrationhistory database, see detailed description here.
On the Mezzanine docs it is stated that createdb is a shortcut for syncdb and migrate commands, see here for detailed description.
Hope this helps!

Django to work with south requires MySQLdb

I am following the django instruction to learn django in eclipse.
I came to the part of running cmd
python manage.py migrate
and it complains about unknown command migrate.
Googled. Knew that it requires South module to be included. I downloaded/installed south, and added 'south' in the INSTALLED_APPS.
I ran the command again, this time it complains
import MySQLdb as Database
ImportError: No module named 'MySQLdb'
So I looked for MySQLdb, only to find that there is none for python 3.
I could not find anything useful. So what do you do to make django to work with mysql?
I know there're other connectors around, but I am trying to follow the django tutorial and it seems that 'migrate' cmd must use 'south' and 'south' must use MySQLdb(?)
--- update ---
Here is the DB settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '******',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
I suspect 'ENGINE' has to be something else, but I failed to find enough information online to figure it out...
You can switch to any database you want MySQL or postgresql or sqlite etc for your django app. South uses the default database engine from your django setting DATABASES. As stated here
South automatically exposes the correct set of database API operations
as south.db.db; it detects which database backend you’re using from
your Django settings file.

Facing issue with South and Django "There is no South database module 'south.db.postgresql_psycopg2' for your database."

I am try to develop one project in Python and Django using South for database migration.
I am very new to south and currently facing one problem when try to run any command syncdb or runserver that is :
"There is no South database module 'south.db.postgresql_psycopg2' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS."
I am using following versions
Django==1.4.2,
South==0.7.6,
psycopg2==2.4.5
Postgres 9.2.2
Python==2.7.1 for the configuration.
I am trying to configure this project on mac machine 10.7.5
Database Settings info:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxxx',
'HOST': '',
'PORT': '',
}
}
SOUTH_DATABASE_ADAPTERS = {
'default': 'south.db.postgresql_psycopg2',
}
Thanks in Advance
Regards,
AnshJ
Its very sill mistake by me, I have 2 different version of south in my project path and one of south didn't have postgresql_psycopg2.py file under db folder and which actually get loaded.
Thanks,
Regards,
AnshJ