python manage.py migrate 出错 django.db.utils.ProgrammingError
2017-03-17 06:55
615 查看
django migrate 出错 django.db.utils.ProgrammingError: (1146, u"Table 'myblog2.blog_article' doesn't exist")
在用django写一个博客系统时,自动创建数据库表格,出现上述错误
Migrations for 'blog':
blog/migrations/0011_auto_20170317_0608.py:
- Alter field upload_time on article
python manage.py migrate
。。。
django.db.utils.ProgrammingError: (1146, u"Table 'myblog2.blog_article' doesn't exist")
----------------------------------------------
原因是myblog2.blog_article这个表格的字段发生了修改
我是用如下方法解决的:
1、删除mysql中的数据库myblog2
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 176
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| awesome |
| djangohotel |
| miniblog |
| myblog |
| myblog2 |
| mysql |
| signup_test |
| signup_test1 |
| signup_test2 |
| signup_test3 |
| signup_test4 |
| sugnup_test2 |
| test |
+--------------------+
14 rows in set (0.03 sec)
mysql> drop database myblog2;
Query OK, 13 rows affected (0.26 sec)
mysql> Ctrl-C -- exit!
Aborted
2、创建采用pymysql自动生成数据库的createdatabase.py文件:
4、makemigrations 和 migrate :
5、总结:估计不删除数据库,只删除那个改动字段的表,也能成功migrate
在用django写一个博客系统时,自动创建数据库表格,出现上述错误
python manage.py makemigrations
Migrations for 'blog':
blog/migrations/0011_auto_20170317_0608.py:
- Alter field upload_time on article
python manage.py migrate
。。。
django.db.utils.ProgrammingError: (1146, u"Table 'myblog2.blog_article' doesn't exist")
----------------------------------------------
原因是myblog2.blog_article这个表格的字段发生了修改
我是用如下方法解决的:
1、删除mysql中的数据库myblog2
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 176
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| awesome |
| djangohotel |
| miniblog |
| myblog |
| myblog2 |
| mysql |
| signup_test |
| signup_test1 |
| signup_test2 |
| signup_test3 |
| signup_test4 |
| sugnup_test2 |
| test |
+--------------------+
14 rows in set (0.03 sec)
mysql> drop database myblog2;
Query OK, 13 rows affected (0.26 sec)
mysql> Ctrl-C -- exit!
Aborted
2、创建采用pymysql自动生成数据库的createdatabase.py文件:
#coding:utf-8 #!/usr/bin/env python import os import re import sys import pymysql from importlib import import_module # search the dirname of settings.py and import it with open('manage.py') as f: s = f.read() d = re.search(r'DJANGO_SETTINGS_MODULE.*?,\s*"(.+?)\.settings', s).group(1) assert 'settings.py' in os.listdir(d) mo = import_module('{d}.settings'.format(d=d)) def getconf(alias='default'): dbconf = mo.DATABASES.get(alias) config = {'host': dbconf.get('HOST'), 'user': dbconf.get('USER'), 'passwd': dbconf.get('PASSWORD'), 'port': dbconf.get('PORT'), 'charset': 'utf8', } config = {k: v for k, v in config.items() if v is not None} db_name = dbconf.get('NAME') return config, db_name def creat_db(config, db_name): try: conn = pymysql.connect(**config) cur = conn.cursor() if '-d' in sys.argv: cur.execute('drop database {}'.format(db_name)) print('success to execute `drop database {};`'.format(db_name)) command = ('create database {} DEFAULT CHARACTER ' 'SET utf8 COLLATE utf8_general_ci').format(db_name) cur.execute(command) print('success to execute `{};`'.format(command)) # conn.select_db(database) conn.commit() cur.close() conn.close() except Exception as e: print("SQL Error: {e}".format(e=e)) def main(): creat_db(*getconf()) # creat_db(*getconf('mysql_property')) main()3、执行该文件:
chmod +x createdatabase.py ./createdatabase.py
4、makemigrations 和 migrate :
python manage.py makemigrations python manage.py migrate
5、总结:估计不删除数据库,只删除那个改动字段的表,也能成功migrate
相关文章推荐
- django python manage.py migrate遇到的错误
- Django中修改DATABASES后,执行python manage.py ****报错!UnicodeEncodeError
- django.db.utils.ProgrammingError: (1146, u"Table'' doesn't exist")解决办法
- django.db.utils.ProgrammingError: (1146, u“Table‘’ doesn’t exist”)问题的解决
- 【Django2.0】python manage.py makemigrations 和 python manage.py migrate的区别
- django.db.utils.ProgrammingError: (1146, "Table 'test_b.products_category' doesn't exist")
- Django执行python manage.py syncdb时,出行No module named MySQLdb问题解决方法
- django-关于manage.py migrate无效的问题
- django操作mysql时django.db.utils.OperationalError: (2003, "Can't connect to MySQL server")异常的解决方法
- django.db.utils.OperationalError: no such table: auth_user
- django.db.utils.OperationalError: (1045, "Access denied for user 'ODBC'@'localhost' (using password)
- 解决django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")
- 玩django时运行python manage.py runserver时遇到问题
- centos7解决django.db.utils.OperationalError: FATAL: Peer authentication failed for user "postgres"
- django.db.utils.OperationalError
- django.db.utils.OperationalError: (1060, "Duplicate column name 'XXX')
- Django学习(1)——python manage.py startapp app-name新建app报错问题
- django 运行python manage.py sqlall books 时报错 app has migration
- python setup.py安装出错: error: Unable to find vcvarsall.bat
- django中的一个数据库错误 django.db.utils.InternalError: 1366