django migrate 出错 django.db.utils.ProgrammingError: (1146, u"Table 'myblog2.blog_article' doesn't exist")
在用django写一个博客系统时,自动创建数据库表格,出现上述错误
python manage.py makemigrations
python manage.py migrate
mysql -u root -p
#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
python manage.py makemigrations
python manage.py migrate