mezgani mezgani - 4 months ago 25
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 db_create.py

The db is already created but I can not create tables from models.py using db_create.py

Here is the db_create.py

#!/usr/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from app import db
import os.path
db.create_all()


Here is my models.py

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
self.email = 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(self.id)

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 config.py

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


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


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



class StagingConfig(Config):
DEVELOPMENT = True
DEBUG = True


class DevelopmentConfig(Config):
DEVELOPMENT = True
DEBUG = True


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


Here is my app.py

#!/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 werkzeug.security import generate_password_hash, check_password_hash
import os


app = Flask(__name__)
app.config.from_pyfile('config.py')
db = SQLAlchemy(app)

@app.route("/data")
#@login_required
def data():
return jsonify(get_data())


@app.route("/")
@login_required
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'])
db.session.add(user)
db.session.commit()
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'))



@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))


if __name__ == '__main__':

app.secret_key = "An6cGY8ezvAcoZ5pGJuBU9vY0GC1XWjfDg6za7oGd76wVMTCFL"

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

port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)


And here is core.py

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()


All the files reside in the same directory

Answer

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

But in app.py 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 app.py. It knows about the models. You should do something like this:

from core import db
...
db.init_app(app)