python manage.py syncdb和python manage.py validate排障

百里弘致
2023-12-01

问题1:
# python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute
    django.setup()
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 124, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 325, in add_to_class
    value.contribute_to_class(cls, name)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 30, in <module>
    'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb.
Did you install mysqlclient or MySQL-python?

解决方法:
# pip install mysqlclient
Collecting mysqlclient
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4d/38/c5f8bac9c50f3042c8f05615f84206f77f03db79781db841898fde1bb284/mysqlclient-1.4.4.tar.gz (86kB)
    100% |████████████████████████████████| 92kB 1.8MB/s 
Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... done
  Stored in directory: /root/.cache/pip/wheels/2d/56/30/f0685c93882b41ed55d1b2d1b587d8090a0c15a42963a4e85c
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.4.4

问题:2:
# python manage.py validate
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute
    django.setup()
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 103, in <module>
    class Group(models.Model):
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 162, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 325, in add_to_class
    value.contribute_to_class(cls, name)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1648, in contribute_to_class
    self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1104, in create_many_to_many_intermediary_model
    'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to},
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/utils/functional.py", line 162, in __mod__
    return six.text_type(self) % rhs
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/utils/functional.py", line 119, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 89, in ugettext
    return _trans.ugettext(message)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 345, in ugettext
    return do_translate(message, 'ugettext')
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 321, in do_translate
    _default = _default or translation(settings.LANGUAGE_CODE)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 228, in translation
    _translations[language] = DjangoTranslation(language)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 134, in __init__
    raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)
IOError: No translation files found for default language zh-CN.

解决方法:
将setting.py中,LANGUAGE_CODE = 'zh-CN'改为LANGUAGE_CODE = 'zh-Hans'

问题3:
# python manage.py validate
Unknown command: 'validate'
Type 'manage.py help' for usage.

解决方法:
查询django版本过高,重装django版本。
# python -m django --version
1.11.23
# pip uninstall django
# pip install django==1.6

问题解决:
# python manage.py validate
0 errors found

问题4:
# python manage.py syncdb
Creating tables ...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 112, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/core/management/sql.py", line 216, in emit_post_sync_signal
    interactive=interactive, db=db)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 185, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
    ctype = ContentType.objects.db_manager(db).get_for_model(klass)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 47, in get_for_model
    defaults = {'name': smart_text(opts.verbose_name_raw)},
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/manager.py", line 154, in get_or_create
    return self.get_queryset().get_or_create(**kwargs)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/query.py", line 388, in get_or_create
    six.reraise(*exc_info)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/query.py", line 380, in get_or_create
    obj.save(force_insert=True, using=self.db)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/query.py", line 1511, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 898, in execute_sql
    cursor.execute(sql, params)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 209, in execute
    res = self._query(query)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 315, in _query
    db.query(q)
  File "/root/python/second_program/venv/lib/python2.7/site-packages/MySQLdb/connections.py", line 226, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xE7\\x94\\xA8\\xE6\\x88\\xB7...' for column 'name' at row 1")

解决方法:
数据库字符集不正确,修改为utf8。
(1)查询并修改数据库字符集:
mysql> show create database oms;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| oms      | CREATE DATABASE `oms` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+----------------------------------------------------------------+

mysql> alter database oms character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> show create database oms;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| oms      | CREATE DATABASE `oms` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+

(2)查询并修改表字符集:
mysql> show create table asset_hostlist;
+-
| asset_hostlist | CREATE TABLE `asset_hostlist` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(20) NOT NULL,
  `hostname` varchar(30) NOT NULL,
  `product` varchar(20) NOT NULL,
  `application` varchar(20) NOT NULL,
  `idc_jg` varchar(10) NOT NULL,
  `status` varchar(10) NOT NULL,
  `remark` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---
1 row in set (0.00 sec)

mysql> alter table asset_hostlist convert to CHARACTER SET utf8;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table asset_hostlist \G
*************************** 1. row ***************************
       Table: asset_hostlist
Create Table: CREATE TABLE `asset_hostlist` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(20) NOT NULL,
  `hostname` varchar(30) NOT NULL,
  `product` varchar(20) NOT NULL,
  `application` varchar(20) NOT NULL,
  `idc_jg` varchar(10) NOT NULL,
  `status` varchar(10) NOT NULL,
  `remark` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

问题解决:
# python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

 类似资料: