migrating to mysql in django - python

I want to migrate from sqlite3 to MySQL in Django. First I used below command:
python manage.py dumpdata > datadump.json
then I changed the settings of my Django application and configured it with my new MySQL database. Finally, I used the following command:
python manage.py loaddata datadump.json
but I got this error :
integrityError: Problem installing fixtures: The row in table
'django_admin_log' with primary key '20' has an invalid foregin key:
django_admin_log.user_id contains a value '19' that does not have a
corresponding value in auth_user.id.

You have consistency error in your data, django_admin_log table refers to auth_user which does not exist. sqlite does not enforce foreign key constraints, but mysql does. You need to fix data and then you can import it into mysql.

I had to move my database from a postgres to a MySql-Database.
This worked for me:
Export (old machine):
python manage.py dumpdata --natural --all --indent=2 --exclude=sessions --format=xml > dump.xml
Import (new machine):
(note that for older versions of Django you'll need syncdb instead of migrate)
manage.py migrate --no-initial-data
Get SQL for resetting Database:
manage.py sqlflush
setting.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'asdf',
'USER': 'asdf',
'PASSWORD': 'asdf',
'HOST': 'localhost',
#IMPORTANT!!
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}
python manage.py loaddata dump.xml

Related

Connecting Django to Postgres: django.db.utils.OperationalError: FATAL: database "DATABASENAME" does not exist

I have just started learning Django after I took some tutorials for Python.
I am trying to connect my POSTGRES database to the Django project I have just created.
However, I am experiencing this problem:
django.db.utils.OperationalError: FATAL: database "producthuntdb" does not exist
I followed these steps:
1) Opened postgress by clicking its icon
2) Clicked on the database "postgress". The terminal opened and I wrote: CREATE DATABASE producthuntdb; The database has been created because I see it if I open postgress via its icon.
3) Went to my Django project in "settings" and change the SQLITE database to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'producthuntdb',
'USER': 'mymac2017',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
4) Run the code python3 manage.py migrate
However, I am getting the error:
django.db.utils.OperationalError: FATAL: database "producthuntdb" does not exist
So I did the following:
Cliking on postgress icon and opening the the database producthuntdb
Once the terminal is open, I wrote: \du
There are two users with the attributes:
1) mymac2017 | Superuser, Create role, Create DB | {}
2) postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
What am I doing wrong?
I tried to look other answers to this problem and most of the issues are from misspelling the database name OR not creating it. However, the name of my database is correct and I can see the database producthuntdb if I open postgres.
Many thanks for your help.
I set the wrong port of the Database in the Django "settings.py".
I was using 'PORT': '5432' instead of 'PORT': '5433'
SOLUTION:
Go to your Postgres app/icon/program
Click on "Server setting" and see which PORT the database POSTGRES is using
Be sure that the PORT the database is using is the same as the one you write in the "settings.py" of your Django project.

Django: How to set the correct the location for MySQL database

I am just starting to use MySQL as the database for my project. Previously I had been using SQLite.
I am wondering how to specify the location for the MySQL database the same way I was able to for SQLite. Currently it saves automatically to /usr/local/mysql/data by default. But I believe this will cause issues when I try to upload it to my production envroment.
My old SQLite settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'database/db.sqlite3'),
}
}
This saved the database into a database folder in my project which I was able to upload and chown both to www-data. This seems like a simple solution which I would like to replicate with MySQL
My new MySQL settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
#'NAME': 'django_db',
'NAME': os.path.join(BASE_DIR, 'database/django_db'),
'USER': 'root',
'PASSWORD': 'myPassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
However when I try to syncdb with this I get the below error
django.db.utils.OperationalError: (1059, "Identifier name
'/users/user/workspace/bias_experiment/src/database/django_db'
is too long") (bias_experiment)localhost:src user$
I also tried to create the database within my project with
mysql> CREATE DATABASE /Users/user/workspace/bias_experiment/src/database/django_db;
But this gave me the error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/Users/brendan/Dropbox/workspace/bias_experiment/src/database/django_db' at line 1
mysql>
So can I create the DB with in my project and link to it in the same way as I did previously with SQLite? And If not what is the correct way/location to upload it to and link to it?
Any help is greatly appreciated
MySQL is not a file-based database: you don't give it a file path. In fact the settings file itself is quite clear about what you need to put in the NAME attribute, ie (not surprisingly) the name of the database itself. This is the same as you need for CREATE DATABASE, but again you don't give that a path: just a name.
You cannot access mysql files directly. You must create a database (give it a name, not a path) and access it.
SQL> CREATE DATABASE django_db

django-mongodb-engine and Django's native auth system

As stated in the title, I am using django-mongodb-engine and I am attempting to configure the native Django authentication framework. I've read some comments online that it should work out of the box sans some features. However, I couldn't find any tutorials and, furthermore, I am getting errors on trying to set it up on my own. The issue I'm having most certainly has to do with database permissions. I have included the Django middleware and apps per the Django docs. However, when I issue the syncdb command it fails with an error.
$ python manage.py syncdb
OperationFailure: database error: not authorized for query on MyDB.system.namespaces
settings.py
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'MyDB',
'USER': 'mySuperUser',
'PASSWORD': 'mypass',
'HOST': 'XXX.XXX.XXX.XXX',
'PORT': '',
},
# some other DBs
}
Mongo User Permissions
myDB> db.system.users.find()
{ "_id" : ObjectId("..."), "user" : "mySuperUser", "pwd" : "...", "roles" : [ "readWriteAnyDatabase", "userAdminAnyDatabase", "dbAdminAnyDatabase", "clusterAdmin" ] }
I'm not sure what other permissions I can grant this guy, and/or where else I need to create this user.
Any ideas?
After playing around, here is the solution. You must use the native mongo admin database. Thus, the required changes:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'admin',
'USER': 'mySuperUser',
'PASSWORD': 'mypass',
'HOST': 'XXX.XXX.XXX.XXX',
'PORT': '',
},
# some other DBs
}
The user mySuperUser must naturally exist on the admin database. To be safe regarding authentication actions such as adding and removing users, I gave it the userAdminAnyDatabase privilege in mongo. The privileges are probably excessive, but I'd have to play with it to determine the proper scope of the required permissions. Here are the permissions:
// mongo
admin> db.system.users.find()
{ "_id" : ObjectId("..."), "pwd" : "...", "roles" : [ "readWriteAnyDatabase", "dbAdminAnyDatabase", "clusterAdmin", "userAdminAnyDatabase" ], "user" : "mySuperUser" }
Next, we can finally run the syncdb command:
$ python manage.py syncdb
Creating tables ...
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'someUser'):
Email address: someUser#user.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installing indices for admin.LogEntry model.
Installing indices for auth.Group_permissions model.
Installing indices for auth.Group model.
Installing indices for auth.User_groups model.
Installing indices for auth.User_user_permissions model.
Installing indices for auth.User model.
Installing indices for sessions.Session model.
Installed 0 object(s) from 0 fixture(s)
$
The problem for me was that I had not specified a SITE_ID in my settings.py. I did this:
./manage.py shell
>>>from django.contrib.sites.models import Site
>>>Site().save()
>>>Site.objects.all()[0].id
u'5391dbc22ebd1212246d50c4'
If you aren't 'django.contrib.sites' then I'm not sure why this would be a problem. Unless you had been using that module and already installed the collection/table to the database. In either case, this is how I got MongoDB to start working correctly again.
I encountered the same issue. My Mongo DB is hosted on MongoLab and I don't find any solutions to solve this error. Although that my user already exists in my DB, I don't want to use the admin database. Someone else has encountered the same problem or find a solution ?

Difficulty opening port 5432 for PostgreSQL on Ubuntu 12.04

I'm trying to get a postgres database talking to a django installation.
I've followed the steps details here: http://blog.iiilx.com/programming/how-to-install-postgres-on-ubuntu-for-django/
However, when I use syncdb to have django update my postgres database, I receive the following error:
connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL: database "/home/flowcore/django_db"
does not exist
django_db is the name of the database and it DOES exist but of course it doesn't exist at /home/flowcore/django_db (that is not where postgres stores data!)
My databases dict in settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.path.join(BASE_DIR, 'django_db'),
'USER': 'django_login',
'PASSWORD': 'mypasswordhere', #obviously i've replaced this with my actual password
'HOST': 'localhost',
}
}
Do I have to specific an absolute path to my postgres database file and, if so, where are these files stored?
Well, for some reason you have put the full path as the NAME parameter, so it's not surprising that Django is using that. There's no reason to do that, and that tutorial you link to doesn't suggest it. NAME is the database name itself, which as you say is just "django_db".

IntegrityError while trying to send a notification with django-notifications

I've installed django-notifications via pip, added the app to my INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.auth',
...
'notifications',
...
)
Updated my URLConf:
import notifications
urlpatterns = patterns('',
...
('^inbox/notifications/', include(notifications.urls), namespace='notifications'),
...
)
Since I have south installed, I migrate the schema:
$ python manage.py migrate notifications
...
...
$ python manage.py migrate --list
notifications
(*) 0001_initial
(*) 0002_auto__add_field_notification_data
(*) 0003_auto__add_field_notification_unread
(*) 0004_convert_readed_to_unread
(*) 0005_auto__del_field_notification_readed
(*) 0006_auto__add_field_notification_level
...
Now, I'm trying to manually create a notification via Django shell but I'm getting an IntegrityError:
[1]: from django.contrib.auth.models import User
[2]: from notifications import notify
[3]: recipient = User.objects.get(username="admin")
[4]: sender = User.objects.get(username="guest")
[5]: notify.send(sender, verb='A test', recipient=recipient)
...
...
...
IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`myapp`.`notifications_notification`, CONSTRAINT `recipient_id_refs_id_5c79cb54` FOREIGN KEY (`recipient_id`) REFERENCES `auth_user` (`id`))')
What's going on? Both, recipient and sender belong to auth_user table. Any help will be much appreciated.
Ok, I found the problem. For some reason south was using InnoDB storage engine as a default while all my tables have MyISAM as a default. This is how I fixed it:
Drop table notifications_notification
Delete all entries of notifications on south_migrationhistory table so you can migrate notifications again later
Add STORAGE_ENGINE to my database settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database',
'USER': 'user',
'PASSWORD': 'pass',
'HOST': '',
'PORT': '',
'STORAGE_ENGINE': 'MyISAM',
}
}
Finally, migrate notifications again:
$ python manage.py migrate notifications

Categories