0

Here's my file structure:

/
/apitestproject
    /models
        __init__.py
        users.py
        items.py
    /resources
        __init__.py
        users.py
        items.py
    __init__.py
    index.py
.deployment
deploy.cmd
requirements.txt
run_waitress_server.py
runserver.py
web.config

Inside my main __init__.py file I have:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

POSTGRES = {
    'user': 'admin_user@pracap',
    'pw': 'the_password',
    'db': 'apitest',
    'host': 'pracap.postgres.database.azure.com',
    'port': '5432',
}
URL = 'postgresql://{}:{}@{}:{}/{}'.format(POSTGRES['user'], POSTGRES['pw'], POSTGRES['host'], POSTGRES['port'], POSTGRES['db'])

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

import apitestproject.index

In my index.py file I have:

from flask import Flask
from flask_restful import Api
from apitestproject import app, db


@app.before_first_request
def create_tables():
    db.create_all()

@app.route('/')
@app.route('/home')
def home():
    return "I'm the default route"

And in my /models/users.py file i have:

from apitestproject import db

class UserModel(db.Model):
    __tablename__ = 'users'

    id = db.column(db.string, primary_key=True)
    name = db.column(db.string(50))
    address = db.column(db.string(144))
    salary = db.column(db.numeric(12, 2))
    position = db.column(db.string(50))
    password = db.column(db.string(50))

The console is not throwing any errors and I can run everything right without even a single hint of an error. But the tables are not created. Any idea what I might be doing wrong? I've been working with flask/sqlalchemy for just over a month now and I'm starting to work with DB's.

Any help would be appreciated!

1
  • 1
    is runserver.py yor running script??? if yes please add this code Commented Sep 12, 2017 at 6:18

1 Answer 1

1

I think you will need to work with flask-migrate as a good alternative : just find this tutorial about it :

a short descrition about it is here :

Migrations allow us to manage changes we make to the models, and propagate these changes in the database. For example, if later on we make a change to a field in one of the models, all we will need to do is create and apply a migration, and the database will reflect the change.

after installing it via

pip install flask-migrate 

edit your init.py like this :

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
POSTGRES = {
    'user': 'admin_user@pracap',
    'pw': 'the_password',
    'db': 'apitest',
    'host': 'pracap.postgres.database.azure.com',
    'port': '5432',
}
URL = 'postgresql://{}:{}@{}:{}/{}'.format(POSTGRES['user'], POSTGRES['pw'], POSTGRES['host'], POSTGRES['port'], POSTGRES['db'])

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)

import apitestproject.index

and then edit your index.py by removing the db creatation you will do it via command line .

from flask import Flask
from flask_restful import Api
from apitestproject import app, db

@app.route('/')
@app.route('/home')
def home():
    return "I'm the default route"

after that go to your application directory and export the flask app and what are the run script of your app?? :

in cmd do this :

export FLASK_APP=your_runScript.py

and then the 2 following commands to migrate the db :

flask db init

to create the migration folder

and :

flask db migrate to create the first migration and :

flask db upgrade 

to upgrade changes to the db

find more here about the flask-migrate package

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.