1. 程式人生 > >3-6 migrations原理及表生成

3-6 migrations原理及表生成

 

執行 

報錯:

Failed to get real commands on module "MxShop": python process died with code 1: Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pycharm\_jb_manage_tasks_provider.py", line 25, in <module>
    django.setup()
  File "D:\Envs\MxShop\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\Envs\MxShop\lib\site-packages\django\apps\registry.py", line 93, in populate
    "duplicates: %s" % app_config.label)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: users

 

解決辦法:重複了,刪除一個

 

 

又報錯:

[email protected] > makemigrations
"C:\Program Files\JetBrains\PyCharm 2018.2.4\bin\runnerw.exe" D:\Envs\MxShop\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pycharm\django_manage.py" makemigrations C:/Users/huang/PycharmProjects/MxShop
Tracking file by folder pattern:  migrations
SystemCheckError: System check identified some issues:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserProfile.groups'.
	HINT: Add or change a related_name argument to the definition for 'User.groups' or 'UserProfile.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserProfile.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'UserProfile.user_permissions'.
users.UserProfile.groups: (fields.E304) Reverse accessor for 'UserProfile.groups' clashes with reverse accessor for 'User.groups'.
	HINT: Add or change a related_name argument to the definition for 'UserProfile.groups' or 'User.groups'.
users.UserProfile.user_permissions: (fields.E304) Reverse accessor for 'UserProfile.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
	HINT: Add or change a related_name argument to the definition for 'UserProfile.user_permissions' or 'User.user_permissions'.

Process finished with exit code 1

解決辦法:

發現下面一句 不知道什麼時候沒有了,在setting裡填上就可以了

AUTH_USER_MODEL = 'users.UserProfile'

 

又報錯:

[email protected] > makemigrations
"C:\Program Files\JetBrains\PyCharm 2018.2.4\bin\runnerw.exe" D:\Envs\MxShop\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pycharm\django_manage.py" makemigrations C:/Users/huang/PycharmProjects/MxShop
Tracking file by folder pattern:  migrations
No changes detected

Process finished with exit code 0
[email protected]
> migrate "C:\Program Files\JetBrains\PyCharm 2018.2.4\bin\runnerw.exe" D:\Envs\MxShop\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pycharm\django_manage.py" migrate C:/Users/huang/PycharmProjects/MxShop Tracking file by folder pattern: migrations System check identified some issues: WARNINGS: ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default' HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-sql-mode Operations to perform: Apply all migrations: auth, contenttypes, goods, sessions, trade, user_operation, users Running migrations: Traceback (most recent call last): File "D:\Envs\MxShop\lib\site-packages\django\db\backends\utils.py", line 83, in _execute return self.cursor.execute(sql) File "D:\Envs\MxShop\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute return self.cursor.execute(query, args) File "D:\Envs\MxShop\lib\site-packages\pymysql\cursors.py", line 170, in execute result = self._query(query) File "D:\Envs\MxShop\lib\site-packages\pymysql\cursors.py", line 328, in _query conn.query(q) File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 516, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result result.read() File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 1066, in read first_packet = self.connection._read_packet() File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 683, in _read_packet packet.check_error() File "D:\Envs\MxShop\lib\site-packages\pymysql\protocol.py", line 220, in check_error err.raise_mysql_exception(self._data) File "D:\Envs\MxShop\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.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 "D:\Envs\MxShop\lib\site-packages\django\db\migrations\recorder.py", line 55, in ensure_schema editor.create_model(self.Migration) File "D:\Envs\MxShop\lib\site-packages\django\db\backends\base\schema.py", line 312, in create_model self.execute(sql, params or None) File "D:\Envs\MxShop\lib\site-packages\django\db\backends\base\schema.py", line 133, in execute cursor.execute(sql, params) File "D:\Envs\MxShop\lib\site-packages\django\db\backends\utils.py", line 100, in execute return super().execute(sql, params) File "D:\Envs\MxShop\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 "D:\Envs\MxShop\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "D:\Envs\MxShop\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "D:\Envs\MxShop\lib\site-packages\django\db\utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "D:\Envs\MxShop\lib\site-packages\django\db\backends\utils.py", line 83, in _execute return self.cursor.execute(sql) File "D:\Envs\MxShop\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute return self.cursor.execute(query, args) File "D:\Envs\MxShop\lib\site-packages\pymysql\cursors.py", line 170, in execute result = self._query(query) File "D:\Envs\MxShop\lib\site-packages\pymysql\cursors.py", line 328, in _query conn.query(q) File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 516, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result result.read() File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 1066, in read first_packet = self.connection._read_packet() File "D:\Envs\MxShop\lib\site-packages\pymysql\connections.py", line 683, in _read_packet packet.check_error() File "D:\Envs\MxShop\lib\site-packages\pymysql\protocol.py", line 220, in check_error err.raise_mysql_exception(self._data) File "D:\Envs\MxShop\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception raise errorclass(errno, errval) 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 "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pycharm\django_manage.py", line 52, in <module> run_command() File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pycharm\django_manage.py", line 46, in run_command run_module(manage_file, None, '__main__', True) File "C:\Users\huang\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 205, in run_module return _run_module_code(code, init_globals, run_name, mod_spec) File "C:\Users\huang\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "C:\Users\huang\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:/Users/huang/PycharmProjects/MxShop\manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "D:\Envs\MxShop\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "D:\Envs\MxShop\lib\site-packages\django\core\management\__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\Envs\MxShop\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "D:\Envs\MxShop\lib\site-packages\django\core\management\base.py", line 353, in execute output = self.handle(*args, **options) File "D:\Envs\MxShop\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "D:\Envs\MxShop\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle fake_initial=fake_initial, File "D:\Envs\MxShop\lib\site-packages\django\db\migrations\executor.py", line 91, in migrate self.recorder.ensure_schema() File "D:\Envs\MxShop\lib\site-packages\django\db\migrations\recorder.py", line 57, 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")) Process finished with exit code 1

解決辦法:

我解決了 需要升級mysql 5.6