Sabbib
Sabbib

Reputation: 47

MySQL Table not created using Flask and PyMySQL

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://[email protected]: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

Upvotes: 0

Views: 759

Answers (1)

Igor
Igor

Reputation: 2919

In settings.py, try replacing

SQLALCHEMY_DB_URI = DB_URI

with

SQLALCHEMY_DATABASE_URI = DB_URI

And DB_URI = 'mysql+pymysql://[email protected]:blog'

with

DB_URI = 'mysql+pymysql://[email protected]/blog'

according to connection URI format:

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

Upvotes: 1

Related Questions