How do you configure Django 3 to work with MySql 5? - python

I'm using Mac 10.13.6 and MySql 5.5 (can't upgrade at this time). I have just created a skeleton Django (v 3.0.2)/Python 3.7 app and configured my database setting like so
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'sql_mode': 'traditional',
},
'NAME': 'maps_data',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
I have used pip to install the mysqlclient ...
(env) localhost:maps davea$ pip install mysqlclient
Requirement already satisfied: mysqlclient in /Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages (1.4.6)
However, when I attempt to run the initial migrations created for me, I get these errors I don't understand ...
(env) localhost:maps davea$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 315, in _query
db.query(q)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/connections.py", line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.ProgrammingError: (1064, "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 '(6) NOT NULL)' at line 1")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 67, in ensure_schema
editor.create_model(self.Migration)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 324, in create_model
self.execute(sql, params or None)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 74, in execute
return self.cursor.execute(query, args)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/cursors.py", line 315, in _query
db.query(q)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/MySQLdb/connections.py", line 239, in query
_mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1064, "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 '(6) NOT NULL)' at line 1")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 233, in handle
fake_initial=fake_initial,
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 91, in migrate
self.recorder.ensure_schema()
File "/Users/davea/Documents/workspace/chicommons/maps/env/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 69, in ensure_schema
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "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 '(6) NOT NULL)' at line 1"))
What are the right versions of Django and mysqlclient to use, or what else do I need to do to configure things properly?

MySQL 5.5 was dropped in Django 2.1. Here's the changelog.
So the official answer would be to upgrade your old version of MySQL.
...That said, I had a migration fail mid-execution in production because of this, and I was able to resolve it using the following process:
Obtain the raw sql that the migration would run with python manage.py sqlmigrate myapp 0015
Where 'myapp' is the name of your app, and '0015' is the migration number you're trying to run.
Adjust the query as required.
In this case; replace occurrences of 'datetime(6)' with 'datetime'
Run the new query manually.
Tell django that the migration has been run manually by calling python manage.py migrate myapp 0015 --fake

Related

Django Heroku users_customuser table can't migrate

I have a Django project I've been developing locally for a while. It works fine on my local machine, and I am using a customuser setup so that my login auth only needs email / password, as opposed to the default Django user table which needs email / username / password.
I just got my project hosted on Heroku, and can see the webpage UI get loaded fine. But I'm getting Django errors when clicking any links, and think its connected to an issue when I try to run
heroku run python manage.py migrate
I'm trying to run this on heroku to setup the DB, but when I do so I get an error saying:
Running python manage.py migrate on ⬢ songsweeper... up, run.1353 (Free)
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, playlist, sessions, sites
Running migrations:
Applying account.0001_initial...Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "users_customuser" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 247, in apply_migration
migration_recorded = True
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 110, in __exit__
self.execute(sql)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "users_customuser" does not exist
My project is very large, but to give an overview of where I'm defining customuser, it appears in these places:
Does anyone have any advice on how to fix this migration error? Or is it indicative or something alrger? The 'sign-in' link on my site leads to a different error saying "relation "django_site" does not exist", but I think it has something to do with this failed customuser error.
I am currently using the default sqlite3 db.
I faced the problem similar to yours before.
In my opinion, I think it is because of a migration file in the user app.
So you will have to do migrate the user model.
$ python manage.py makemigrations users
$ python manage.py migrate
I resolved the problem using the above way.

Unable to migrate models from django to sql server

I'm trying to learn django and have to use sql server as a data base and unable to migrate models using manage.py migrate
I'm using django 2.1.8
Pyodbc
python 3.7.3
I've tried to reinstall django, python and even sql server but it didn't solve the problem.
On running py manage.py migrate, i get the following error
py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, testApp
Running migrations:
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\models.py", line 514, in database_forwards
getattr(new_model._meta, self.option_name, set()),
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 360, in alter_unique_together
self.execute(self._create_unique_sql(model, columns))
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\schema.py", line 653, in execute
cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Hamza\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S02', '[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find the object "django_content_type" because it does not exist or you do not have permissions. (4902) (SQLExecDirectW)')
I expect it to migrate but it gives me this error.
Might be a different problem, but I was having the same error. In my case I had run python manage.py migrate --fake (per other SO threads) to fix an error I had earlier and django made a record of the migration without actually creating the table. When I ran it again it assumed I was modifying a table that wasn't there.
I eventually solved this by:
Manually creating the table in SQL server with all of the correct fields
Commenting out the model code from everywhere in the models.py
python manage.py makemigrations to remove the fake table from registry
python manage.py migrate to delete the manual table I just made and update the migrations registry
then re-add the table to my models.py
then re-run python manage.py makemigrations
python manage.py migrate to sync django with SQL server

Django test database username

I am starting a Django project and I am using gitlab's ci/cd on the shared runners and I use Postgres as the database.
I have this weird problem that it seems like Django is creating the test database with the username "postgres" and I can't find a way to configure it's setting and change it to use the role named "runner". This causes a break in my ci/cd pipeline.
here is my .gitlab-ci.yml:
image: python:3.6.5
services:
- postgres:latest
variables:
POSTGRES_DB: asdproject
POSTGRES_USER: runner
POSTGRES_PASSWORD: asdpassword
test:
script:
- whoami
- export DATABASE_URL=postgres://postgres:#postgres:5432/asdproject
- export PGPASSWORD=$POSTGRES_PASSWORD
- apt-get update -qy
- apt-get install -y python-dev python-pip
- pip install -r requirements.txt
- python manage.py test - settings=asd.gitlab_runner_settings
and my gitlab_runner_settings.py file:
I tried many forms of changing settings.py that were recommended in questions but neither worked.
from asd.with_debug_settings import *
import sys
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'asdproject',
'USER': 'runner',
'PASSWORD': 'asdpassword',
'HOST': 'postgres',
'PORT': '5432',
'TEST': {
'NAME': 'asdtest',
'USER': 'runner'
},
}
}
The error I get while running my pipeline script in gitlab is:
---------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Using existing test database for alias 'default'...
System check identified no issues (0 silenced).
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
psycopg2.ProgrammingError: role "postgres" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/test.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/test.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "/usr/local/lib/python3.6/site-packages/django/test/runner.py", line 607, in run_tests
self.teardown_databases(old_config)
File "/usr/local/lib/python3.6/site-packages/django_heroku/core.py", line 41, in teardown_databases
self._wipe_tables(connection)
File "/usr/local/lib/python3.6/site-packages/django_heroku/core.py", line 33, in _wipe_tables
"""
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: role "postgres" does not exist
ERROR: Job failed: exit code 1
You need to have a host
'HOST': 'postgres',
this is wrong
It should be either 'localhost' or the address of remote server ip
The error occurs because when gitlab-ci runs tests it creates database according to the settings set in you yml file:
variables:
POSTGRES_DB: asdproject
POSTGRES_USER: runner
POSTGRES_PASSWORD: asdpassword
So runner role and asdproject database is created.
But you set your database url as DATABASE_URL=postgres://postgres:#postgres:5432/asdproject
Where postgres user is set.
Please try DATABASE_URL=postgres://runner:asdpassword#postgres:5432/asdproject

Getting a SQL error when running "python manage.py migrate" using a MySQL database

I've been through the introductory tutorial for Django successfully using SQLite as per the instructions. However, for my real project I want to use MySQL, so I deleted the tutorial project files and started with a fresh setup. The trouble is that when I run "python manage.py migrate" I get the following MySQL error:
_mysql_exceptions.ProgrammingError: (1064, "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 '%s' at line 1").
I have the following in the site settings database section:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'vocabulator$database',
'USER': 'vocabulator',
'PASSWORD': '<password>',
'HOST': 'mysql.server',
}
}
I connected to this database successfully in Bash using, 'mysql --user=vocabulator --host=mysql.server --password=<password> "vocabulator\$database" ', so it appears I have entered these setting details correctly.
I also followed an instruction on setting up database bindings for Python 3, which also appeared to work successfully. It was on either the Django documentation pages or the PythonAnywhere equivalent, but unfortunately I cannot locate the page reference again.
I haven't written any MySQL queries myself yet, so any incorrect ones that are being made must be coming from manage.py, so what could be the cause? The complete error trace is quoted below:
Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: contenttypes, sessions, admin, auth Synchronizing apps without migrations: Creating tables...
Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial...Traceback (most recent call last): File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 184, in execute
self.errorhandler(self, exc, value) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
raise errorvalue File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 171, in execute
r = self._query(query) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 330, in _query
rowcount = self._do_query(q) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 294, in _do_query
db.query(q)
_mysql_exceptions.ProgrammingError: (1064, "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 '%s' at line 1")
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute() File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 222, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
schema_editor.create_model(model) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 289, in create_model
self.deferred_sql.extend(self._model_indexes_sql(model)) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/schema.py", line 55, in _model_indexes_sql
self.connection.cursor(), model._meta.db_table File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/introspection.py", line 142, in get_storage_engine
"WHERE table_name = %s", [table_name]) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 184, in execute
self.errorhandler(self, exc, value) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
raise errorvalue File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 171, in execute
r = self._query(query) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 330, in _query
rowcount = self._do_query(q) File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-packages/MySQLdb/cursors.py", line 294, in _do_query
db.query(q) django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v ersion for the right syntax to use near '%s' at line 1") Exception ignored in: <bound method Cursor.__del__ of <MySQLdb.curso rs.Cursor object at 0x7feec394b940>> Traceback (most recent call last): File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-p ackages/MySQLdb/cursors.py", line 67, in __del__ File "/home/vocabulator/.virtualenvs/django18/lib/python3.4/site-p ackages/MySQLdb/cursors.py", line 73, in close ReferenceError: weakly-referenced object no longer exists (django18)03:29 ~/mysite $ mysql --user=vocabulator --host=mysql.server --p mysql: ambiguous option '--p' (pager, plugin_dir)
As I said in my comment above, I found the page which had instructed me on how to install drivers. It is this one:
https://www.pythonanywhere.com/wiki/UsingMySQL
However, this Wiki page is a little dated now and it instructed me to install the MySQL connector version 2.0.1. The current version is now 2.0.4. The old version had a bug which meant it would not operate with Django 1.8, as stated here:
http://bugs.mysql.com/bug.php?id=76752
By version 2.0.4 this bug appears to have been corrected.
The line in the PythonAnywhere Wiki page referenced above should change to:
pip3.4 install https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.4.tar.gz
Once I ran this, the migration appears to have worked successfully on my Django installation.
i have try this,and works...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database',
'USER': 'vocabulator',
'PASSWORD': '<password>',
'HOST': 'mysql.server',
}
}

Read-only Oracle database as default

In the Django 1.7 documentation, it is stated that the Django user must have a number of CREATE privileges in order to use the new python manage.py migrate command. This obviously assumes that the Django project requires writing to the database. What if it doesn't? What if I don't want to use migrate?
I am starting a project where I will be using an Oracle database, but do not intend to give any write privileges to Django. I was surprised to see that I can't run python manage.py runserver even when the project is empty, and I just have a settings.py file. Instead, I get the following error message:
django.db.utils.DatabaseError: ORA-01031: insufficient privileges
How can I work around this?
[EDIT]
Also interesting, if I leave Oracle as the default database and head on to the shell, I can actually make queries normally.
[EDIT 2] Traceback...
[myproject]yourstruly#myserver /projects/django/myproject2 $ python manage.py runserver 0.0.0.0:8001Performing system checks...
System check identified no issues (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x1ce36e0>
Traceback (most recent call last):
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/utils/autoreload.py", line 222, in wrapper
fn(*args, **kwargs)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
self.check_migrations()
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 159, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
self.loader = MigrationLoader(self.connection)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__
self.build_graph()
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/migrations/loader.py", line 179, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 53, in ensure_schema
editor.create_model(self.Migration)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/backends/schema.py", line 270, in create_model
self.execute(sql, params)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/backends/schema.py", line 98, in execute
cursor.execute(sql, params)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/projects/.virtualenv/myproject/lib/python2.7/site-packages/django/db/backends/oracle/base.py", line 916, in execute
return self.cursor.execute(query, self._param_generator(params))
django.db.utils.DatabaseError: OprojectsA-01031: insufficient privileges

Categories