2

syncdb fails when creating super user

Django: v 1.2.4 Python: 2.6 MySQL Server: 5.5 Windows 7 Extra: MySQL-Python v1.2.3

What steps will reproduce the problem? 1. install the above programs 2. create a project 3. run syncdb

Note: I have installed mySQL to support UTF 8. I also create the mysite_db database using CREATE DTABASE mysite_db CHARACTER SET = UTF8;

What is the expected output? What do you see instead?

syncdb create the required tables as follows:


C:\DjangoProjects\mysite>python manage.py syncdb
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no):

I select 'YES' and get the following error:


Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
    utility.execute()
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 191,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 351,
 in handle
    return self.handle_noargs(**options)
  File "C:\Python26\lib\site-packages\django\core\management\commands\syncdb.py"
, line 103, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "C:\Python26\lib\site-packages\django\core\management\sql.py", line 182,
in emit_post_sync_signal
    interactive=interactive, db=db)
  File "C:\Python26\lib\site-packages\django\dispatch\dispatcher.py", line 172,
in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\Python26\lib\site-packages\django\contrib\auth\management\__init__.py
", line 44, in create_superuser
    call_command("createsuperuser", interactive=True)
  File "C:\Python26\lib\site-packages\django\core\management\__init__.py", line
166, in call_command
    return klass.execute(*args, **defaults)
  File "C:\Python26\lib\site-packages\django\core\management\base.py", line 220,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python26\lib\site-packages\django\contrib\auth\management\commands\cr
eatesuperuser.py", line 71, in handle
    User.objects.get(username=default_username)
  File "C:\Python26\lib\site-packages\django\db\models\manager.py", line 132, in
 get
    return self.get_query_set().get(*args, **kwargs)
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 342, in g
et
    num = len(clone)
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 80, in __
len__
    self._result_cache = list(self.iterator())
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 271, in i
terator
    for row in compiler.results_iter():
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 67
7, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 73
2, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86
, in execute
    return self.cursor.execute(query, args)
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 175, in execute
    if not self._defer_warnings: self._warning_check()
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 89, in _warning_
check
    warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Incorrect string value: '\xED' for column 'username'
at row 1

What version of the product are you using? On what operating system?

Django: v 1.2.4 Python: 2.6 MySQL Server: 5.5 Windows 7 Extra: MySQL-Python v1.2.3

Please provide any additional information below.

I also have mySQL C++ and ODBC database connectors installed. Any help is greatly appreciated.

2 Answers2

0

Looks like you have unicode characters somewhere in your models.py or a fixture. Convert the files to UTF-8 and add a comment as the very first line:

#coding-utf-8

Also, before unicode strings add u, like this:

name = CharField(maxlength=255, default=u"утф-8 строка");
Silver Light
  • 44,202
  • 36
  • 123
  • 164
  • Thank you. Where will I find the correct models.py file? There are several in the Django directory.. – david_heagney Jan 25 '11 at 16:34
  • Also, is it also models.py that I insert the above piece of code? – david_heagney Jan 25 '11 at 16:40
  • It's `# -*- coding: utf-8 -*-`. – AndiDog Jan 25 '11 at 17:55
  • Hi AndiDog - which file do I insert this into? I have found numerous 'models.py' files. Thanks. – david_heagney Jan 25 '11 at 18:00
  • @david_heagney: Forgot to mention - I don't think that this will solve your problem. The Python parser would normally give you a `SyntaxError` if you have special characters in the file (because it defaults to ASCII). If you really defined the username somewhere with special characters, and defined the wrong encoding, you have to correct that file. – AndiDog Jan 25 '11 at 18:12
  • @AndiDog: I haven't defined the username anywhere. The only time a specified any username/password was when setting up mySQL - it asks for a password for 'localhost' during installation. – david_heagney Jan 25 '11 at 18:31
0

It looks like your login on your development machine has a unicode character in it, and when the createsuperuser.py module looks for a User object with that username, it is barfing because MySQL isn't expecting those characters. You can either change the collation settings on your MySQL tables to accept UTF-8 or use a username that doesn't have unicode characters (which isn't a very nice option).

The real failure in the traceback starts in createsuperuser.py on line 71, and it stems from line 59 here:

        try:
         default_username = getpass.getuser().replace(' ', '').lower()
     except (ImportError, KeyError):
         # KeyError will be raised by os.getpwuid() (called by getuser())
         # if there is no corresponding entry in the /etc/passwd file
         # (a very restricted chroot environment, for example).
         default_username = ''

     # Determine whether the default username is taken, so we don't display
     # it as an option.
     if default_username:
         try:
             User.objects.get(username=default_username)
         except User.DoesNotExist:
             pass
         else:
             default_username = ''

It's getting your username via getpass and then trying to look that up in the User table.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • Quibble: collation determines sort order and what characters compare as equal. What the OP needs to do is to change the db's **charset**. – Daniel Roseman Jan 25 '11 at 15:34
  • 1
    Thanks doe you help. I recreated the database using the following SQL: CREATE DATABASE mysite_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; and got the same error. – david_heagney Jan 25 '11 at 16:48