MySQL表不是使用Flask和PyMySQL创建的 [英] MySQL Table not created using Flask and PyMySQL

查看:209
本文介绍了MySQL表不是使用Flask和PyMySQL创建的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是重复的,但其他问题的解决方案并没有帮助我。



我试图模块化我的代码,我移动我的配置文件到另一个名为settings.py的文件。

运行我要从终端运行的代码python3 manage.py shell
Python没有更新
然后执行

  from flask_blog import db 
from author.models import Author
db。 create_all()







因此问题是:为什么数据库没有被更新?






三个文件我有这样的关心配置:
$ b

manage.py 处理服务器设置(以简单的字符串)
$ b

settings.py 现在处理数据库



models.py 数据库模型


$ b __ init __。py init 文件



下面的代码是 settings.py

  import os 

SECRET_KEY ='现在没有'
DEBUG = True
DB_USERNAME ='root'
DB_PASSWORD ='1234'
BLOG_DB_NAME ='blog2'
DB_HOST = os.getenv('IP','127.0.0.1')
DB_URI ='mysql + pymysql://root@127.0.0.1:blog'
#DB_URI ='mysql + pymysql://%s:%s @%s /%s'%(DB_USERNAME,DB_PASSWORD,DB_HOST, BLOG_DB_NAME)

SQLALCHEMY_DB_URI = DB_URI
SQLALCHEMY_TRACK_MODIFICATIONS = True

其他名为 manage.py (下面)的文件,它处理运行的基本项目配置。

  import os,sys 
sys.path.append(os.path.abspath(os.path.join os.path.dirname(__ file__),'..')))

from flask.ext.script import Manager,Server $ b $ from flask_blog import app

manager = Manager(app)

manager.add_command(runserver,Server(
use_debugger = True,
use_reloader = True,
host = os.getenv 'IP','127.0.0.1'),
port = int(os.getenv('PORT',5000))
))

if __name__ ==__main__ :
manager.run()

最后是下面的数据库模型。 models.py

  from flask_blog import db 

class Author(db (db.teger,primary_key = True)
fullname = db.Column(db.String(80))
email = db.Column(db .String(35),unique = True)
username = db.Column(db.String(80),unique = True)
password = db.Column(db.String(80))
is_author = db.Column(db.Boolean)

def __init __(self,fullname,email,username,password,is_author = False):
self.fullname =全名
self.email =电子邮件
self.username =用户名
self.password =密码
self.is_author = is_author

def __repr __(self):
返回'<作者%r>'%self.username

<__ init__.py 低于

  from flask导入Flask 
from flask.ext.sqlalchemy导入SQLAlchemy


app =烧瓶(__ name__)
来自blog导入视图的
app.config.from_object('settings')
db = SQLAlchemy(app)

作者导入视图

如果您想查看整个项目,那么

解决方案

settings.py ,尝试替换

  SQLALCHEMY_DB_URI = DB_URI 

带有

  SQLALCHEMY_DATABASE_URI = DB_URI 

DB_URI ='mysql + pymysql://root@127.0.0.1:blog'





  DB_URI ='根据连接的URI格式,mysql + pymysql://root@127.0.0.1/blog'

 方言+驱动程序://用户名:密码@主机:端口/数据库


This maybe a duplicate, but all the solutions to the other questions did not help me.

I am trying to modularize my code where I am moving my config files to a separate file called settings.py.

To run the code i am going running from my terminal "python3 manage.py shell" Python not updating then I execute

from flask_blog import db
from author.models import Author
db.create_all()


Hence the QUESTION is: Why is the database not being updated?


The three file I have that concern config are:

manage.py Handles the server set-up (in simple wordds)

settings.py Handles the database for now

models.py The database model

__init__.py The init file

The code is below is the settings.py

import os

SECRET_KEY = 'NOTHING FOR NOW'
DEBUG = True
DB_USERNAME = 'root'
DB_PASSWORD = '1234'
BLOG_DB_NAME = 'blog2'
DB_HOST = os.getenv('IP', '127.0.0.1')
DB_URI = 'mysql+pymysql://root@127.0.0.1:blog'
# DB_URI = 'mysql+pymysql://%s:%s@%s/%s' % (DB_USERNAME, DB_PASSWORD, DB_HOST, BLOG_DB_NAME)

SQLALCHEMY_DB_URI = DB_URI
SQLALCHEMY_TRACK_MODIFICATIONS = True

The other file called manage.py (below) which handles the basic project config to run.

import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from flask.ext.script import Manager, Server
from flask_blog import app

manager = Manager(app)

manager.add_command("runserver", Server(
            use_debugger = True,
            use_reloader = True,
            host = os.getenv('IP', '127.0.0.1'),
            port = int(os.getenv('PORT', 5000))
        ))

if __name__ == "__main__":
    manager.run()

And lastly the database model below. models.py

from flask_blog import db

class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    fullname = db.Column(db.String(80))
    email = db.Column(db.String(35), unique=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(80))
    is_author = db.Column(db.Boolean)

    def __init__(self, fullname, email, username, password, is_author=False):
        self.fullname = fullname
        self.email = email
        self.username = username
        self.password = password
        self.is_author = is_author

    def __repr__(self):
        return '<Author %r>' % self.username

The __init__.py is below

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config.from_object('settings')
db = SQLAlchemy(app)

from blog import views
from author import views

If you want to see the entire project then click here

解决方案

In settings.py, try replacing

SQLALCHEMY_DB_URI = DB_URI

with

SQLALCHEMY_DATABASE_URI = DB_URI

And DB_URI = 'mysql+pymysql://root@127.0.0.1:blog'

with

DB_URI = 'mysql+pymysql://root@127.0.0.1/blog'

according to connection URI format:

dialect+driver://username:password@host:port/database

这篇关于MySQL表不是使用Flask和PyMySQL创建的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆