MySQL Table not created using Flask and PyMySQL

泄露秘密 提交于 2019-12-08 09:23:24

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/42323929/mysql-table-not-created-using-flask-and-pymysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!