mezgani mezgani - 3 months ago 17x
Python Question

Flask does not create tables

I'm trying to create database schema using flask, so I installed sqlalchemy-migrate and Flask-Migrate. and I ran python

The db is already created but I can not create tables from using

Here is the

from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from app import db
import os.path

Here is my

from core import db
from datetime import *

class User(db.Model):
__tablename__= "users"
id = db.Column('user_id', db.Integer, primary_key=True)
username = db.Column('username', db.String(20), unique=True, index=True)
password = db.Column('password', db.String(10))
email = db.Column('email', db.String(50), unique=True, index=True)
registered_on = db.Column('registered_on', db.DateTime)
todos = db.relationship('Todo', backref='user', lazy='dynamic')

def __init__(self, username, password, email):
self.username = username
self.password = password = email
self.registered_on == datetime.utcnow()

def set_password(self, password):
self.password = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.password, password)

def is_authentificated(self):
return True

def is_active(self):
return True

def is_anonymous(self):
return False

def get_id(self):
return unicode(

def __repr___(self):
return '<User %r>' % (self.username)

class Todo(db.Model):
__tablename__ = 'todos'
id = db.Column('todo_id', db.Integer, primary_key=True)
title = db.Column(db.String(255))
text = db.Column(db.String)
done = db.Column(db.Boolean)
#pub_date = db.Column(db.Datetime)
user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'))

def __init__(self, title, text):
self.title = title
self.text = text
self.done = False
self.pub_date = datetime.utcnow()

Here is my

import os
basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:xeeW3kah@db/todo"

class Config(object):
DEBUG = False
SECRET_KEY ="An6cGY8ezvAcoZ5pGJuBU9vY0GC1XWjfDg6za7oGd76wVMTCFL"
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:xeeW3kah@db/todo"

class StagingConfig(Config):
DEBUG = True

class DevelopmentConfig(Config):
DEBUG = True

class TestingConfig(Config):
class ProductionConfig(Config):
DEBUG = False

Here is my

#!/usr/bin/python env

from flask import Flask, session, request, flash, url_for, redirect, render_template, jsonify, abort ,g
from flask_login import login_user, logout_user, current_user, login_required, LoginManager
from stock_scraper import get_data
from models import *
from flask_sqlalchemy import SQLAlchemy
from import generate_password_hash, check_password_hash
import os

app = Flask(__name__)
db = SQLAlchemy(app)

def data():
return jsonify(get_data())

def index():
return render_template("index.html")

@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'GET':
return render_template('register.html')

user = User(request.form['username'], request.form['password'], request.form['email'])
flash('User successfully registred')
return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
username = request.form['username']
password = request.form['password']

if username=='admin' and password=='admin':
flash('Logged in successfully')
return redirect(url_for('data'))
flash('Username or Password is invalid', 'error')
return redirect(url_for('login'))

def logout():
return redirect(url_for('index'))

if __name__ == '__main__':

app.secret_key = "An6cGY8ezvAcoZ5pGJuBU9vY0GC1XWjfDg6za7oGd76wVMTCFL"

login_manager = LoginManager()
login_manager.login_view = 'login'

port = int(os.environ.get('PORT', 5000))'', port=port, debug=True)

And here is

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

All the files reside in the same directory


You create your database connection in You import that in your models module.

But in you are creating a new db object that doesn't know anything about your models:

db = SQLAlchemy(app)

That's what is being imported by your db_create script, so naturally the script has no access to the models.

You should import that same db from core in It knows about the models. You should do something like this:

from core import db