簡體   English   中英

python manage.py migrate 返回 django.db.utils.ProgrammingError: (1064 ... error

[英]python manage.py migrate returns with django.db.utils.ProgrammingError: (1064... error

我是一個新手,我正在嘗試使用mysql數據庫在Windows 8使用Python 3Django 1.8.6創建一個 django 項目

當我執行python manage.py migrate我得到一個錯誤

django.db.utils.ProgrammingError: (1064 ....

我已經交叉檢查了我的 settings.py 文件和所有內容。 事實上,連接已建立,有時它會成功創建表django_content_typedjango_migrations ,然后出現此錯誤,有時沒有創建這些表本身,我會收到此錯誤。

打印的堆棧跟蹤結束如下:

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")

我還提到並嘗試按照本站點其他類似查詢中的說明(已安裝 mysql-connector-python-2.1.3)進行操作,但似乎沒有任何效果

完整的堆棧跟蹤如下:

D:\pythonLearn\learningDjango\mysite>python manage.py migrate
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\mysql\base.py", line 124, in execute return self.cursor.execute(query, args)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 207, in execute self.errorhandler(self, exc, value)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\connections.py", line 37, in defaulterrorhandler raise errorvalue
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 192, in execute r = self._query(query)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 356, in _query rowcount = self._do_query(q)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-md64.egg\MySQLdb\cursors.py", line 320, 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 "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 346, in execute self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv self.execute(*args, **cmd_options)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 445, in execute output = self.handle(*args, **options)
File "C:\Python34\lib\site-packages\django\core\management\commands\migrate.py", line 93, in handle executor = MigrationExecutor(connection, self.migration_progress_callback)
File "C:\Python34\lib\site-packages\django\db\migrations\executor.py", line 19, in __init__self.loader = MigrationLoader(self.connection)
File "C:\Python34\lib\site-packages\django\db\migrations\loader.py", line 47, in __init__self.build_graph()
File "C:\Python34\lib\site-packages\django\db\migrations\loader.py", line 188, in build_graph   self.applied_migrations = recorder.applied_migrations()
File "C:\Python34\lib\site-packages\django\db\migrations\recorder.py", line 59, in applied_migrations self.ensure_schema()
File "C:\Python34\lib\site-packages\django\db\migrations\recorder.py", line 53, in ensure_schema editor.create_model(self.Migration)
File "C:\Python34\lib\site-packages\django\db\backends\base\schema.py", line 289, in create_model self.deferred_sql.extend(self._model_indexes_sql(model))
File "C:\Python34\lib\site-packages\django\db\backends\mysql\schema.py", line 56, in _model_indexes_sql self.connection.cursor(), model._meta.db_table
File "C:\Python34\lib\site-packages\django\db\backends\mysql\introspection.py", line 142, in get_storage_engine
"WHERE table_name = %s", [table_name])
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\utils.py", line 98, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python34\lib\site-packages\django\utils\six.py", line 658, in reraise raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\django\db\backends\utils.py", line 64, in execute return self.cursor.execute(sql, params)
File "C:\Python34\lib\site-packages\django\db\backends\mysql\base.py", line 124, in execute return self.cursor.execute(query, args)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 207, in execute self.errorhandler(self, exc, value)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\connections.py", line 37, in defaulterrorhandler raise errorvalue
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 192, in execute r = self._query(query)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 356, in _query rowcount = self._do_query(q)
File "C:\Python34\lib\site-packages\mysql_python-1.2.4-py3.4-win-amd64.egg\MySQLdb\cursors.py", line 320, 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")

django.db.utils.ProgrammingError: (1064, “您的 SQL 語法有錯誤;請檢查與您的 MySQL 服務器版本相對應的手冊,以獲取在第 1 行附近的 '(6) NOT NULL)' 附近使用的正確語法” )

需要將mysql升級到5.6或更高版本。 這是由於 DATETIME(6) 引起的。 支持 5.6 或 lated 版本。

參考https://docs.djangoproject.com/en/2.1/ref/databases/#fractional-seconds-support-for-time-and-datetime-fields

您的錯誤消息說...

...要在第 1 行的“%s”附近使用正確的語法

這意味着您的程序傳遞給 MySQL 的查詢包含文本字符串%s 在我看來,您的程序沒有將值替換到該字符串中。 它必須在嘗試使用字符串作為查詢之前這樣做。

這種在查詢中使用占位符的業務很常見。 你的查詢有可能是這樣的嗎?

 INSERT INTO names (name) VALUES (%s)

它需要這樣說

 INSERT INTO names (name) VALUES ('Ban Ki Moon')

正如@krishna chandak 所說,由於DATETIME(6) ,您應該升級MySQL版本。

如何更新 MySQL 版本:

確保 yum 是最新的

yum update yum

安裝 yum-utils(它提供 yum-config-manager)

yum install yum-utils

備份:將所有數據庫轉儲到 all_dbs.sql

mysqldump --lock-all-tables --all-databases -uroot -yourpassword >/root/all_dbs.sql

停止 MySQL 守護進程

service mysqld stop

下載最新的 MySQL

------ 適用於 CentOS 6

wget https://dev.mysql.com/get/mysql80-community-release-el6-2.noarch.rpm
sudo rpm -Uvh mysql80-community-release-el6-2.noarch.rpm

------ 適用於 CentOS 7

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm

刪除舊版本並安裝 5.7 版本

sudo yum-config-manager --enable mysql57-community
sudo yum-config-manager --disable mysql80-community
yum remove mysql55-server
yum install mysql57-server
yum update mysql

啟動 MySQL

service mysqld start

如果出現任何錯誤,則必須刪除 etc/my.cnf 中已棄用的變量

升級現有數據庫

mysql_upgrade -uroot -p

如果您使用 *.sock 出錯

通過添加更改配置/etc/my.cnf

datadir=/var/lib/mysql

並評論這個字符串

# socket=/var/lib/mysql/mysql.sock

然后執行更新數據庫的步驟並將配置返回到以前的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM