python manage.py migrate 出错 django.db.utils.ProgrammingError

django migrate 出错 django.db.utils.ProgrammingError: (1146, u"Table 'myblog2.blog_article' doesn't exist")

在用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值