3

I'm trying to follow the "Writing your first Django app" tutorial on the Django website and I'm stuck at part 2 about the database stuff.

I'm running Ubuntu 14.04 and Python 3.4.

I've installed MySQL and the Python MySQL libraries and made a database in MySQL called testdb but when trying to run the command

python3 manage.py migrate

I get the following error:

server1:~/Desktop/app/mysite$ python3 manage.py migrate
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 207, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
    raise errorvalue
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 192, in execute
    r = self._query(query)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 355, in _query
    rowcount = self._do_query(q)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 319, 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 "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/commands/migrate.py", line 93, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/executor.py", line 19, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 47, in __init__
    self.build_graph()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/loader.py", line 182, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/usr/local/lib/python3.4/dist-packages/django/db/migrations/recorder.py", line 53, in ensure_schema
    editor.create_model(self.Migration)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/base/schema.py", line 289, in create_model
    self.deferred_sql.extend(self._model_indexes_sql(model))
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/schema.py", line 55, in _model_indexes_sql
    self.connection.cursor(), model._meta.db_table
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/introspection.py", line 142, in get_storage_engine
"WHERE table_name = %s", [table_name])
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.4/dist-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 207, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/connections.py", line 37, in defaulterrorhandler
    raise errorvalue
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 192, in execute
    r = self._query(query)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 355, in _query
    rowcount = self._do_query(q)
  File "/usr/local/lib/python3.4/dist-packages/MySQLdb/cursors.py", line 319, 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 version for the right syntax to use near '%s' at line 1")

My database setup in django looks like this

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'testdb',
        'USER': 'root',
        'PASSWORD': 'xxxx',
        'HOST': '127.0.0.1',
    }
 }

The only changes I made to the base django project template was adding an app, some url maps and making the change to the database settings and switching the backend to sqlite works fine.

Any help with figuring this out would be awesome, thanks.

Edit: Here's my installed apps, urls and I don't have any models yet.

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

project urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^polls/', include('polls.urls'))
]

app urls.py

urlpatterns = [url(r'^$', views.index, name='index')]
Mike Jones
  • 135
  • 1
  • 10
  • `_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")` - this gives you a hint where the error could reside. It would be helpfull to add the `urls.py`, the part from `settings.py` with `INSTALLED_APPS` and eventually the `models.py`. – cezar Jul 10 '15 at 09:13
  • I added the installed apps and urls, my models.py is completely empty. I know there's a problem with the mysql somewhere but when the mysql is generated by django I have no idea where to begin looking. – Mike Jones Jul 10 '15 at 09:22
  • So, messages `"WHERE table_name = %s", [table_name]` and `syntax to use near '%s'` gave me a thought about empty table name. What do you migrate if `models.py is completely empty` and you `polls` app doesn't exists into INSTALLED_APPS? - Maybe you should to create a basic scheme with syncdb? – Max Vyaznikov Jul 10 '15 at 09:28
  • According to the tutorial it's migrating tables used by some of the installed apps. I tried commenting out all the installed apps and it still gives the same error and doing a syncdb gives a similar error about mysql syntax. – Mike Jones Jul 10 '15 at 09:40

2 Answers2

2

The problem seems to have been a bad MySQL connector. Found a good one from this post.

Had to do a

sudo apt-get install libmysqlclient-dev

then

sudo pip3 install mysqlclient
Community
  • 1
  • 1
Mike Jones
  • 135
  • 1
  • 10
0

Make sure that you have deleted the migration files in all your applications at first

then run make migrations in each app you have

python manage.py makemigrations <appname_1   # till appname_n>

then

python manage.py migrate