python - db.create_all() doesn't throw any errors and doesn't create the tables -
here's 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 main __init__.py
file have:
from flask import flask 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 index.py
file have:
from flask import flask flask_restful import api apitestproject import app, db @app.before_first_request def create_tables(): db.create_all() @app.route('/') @app.route('/home') def home(): return "i'm default route"
and in /models/users.py
file 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 not throwing errors , can run right without single hint of error. tables not created. idea might doing wrong? i've been working flask/sqlalchemy on month , i'm starting work db's.
any appreciated!
i think need work flask-migrate alternative : find this tutorial :
a short descrition here :
migrations allow manage changes make models, , propagate these changes in database. example, if later on make change field in 1 of models, need create , apply migration, , database reflect change.
after installing via
pip install flask-migrate
edit init.py :
from flask import flask flask_sqlalchemy import sqlalchemy 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 edit index.py removing db creatation via command line .
from flask import flask flask_restful import api apitestproject import app, db @app.route('/') @app.route('/home') def home(): return "i'm default route"
after go application directory , export flask app , run script of app?? :
in cmd :
export flask_app=your_runscript.py
and 2 following commands migrate db :
flask db init
to create migration folder
and :
flask db migrate create first migration , :
flask db upgrade
to upgrade changes db
find more here flask-migrate package
Comments
Post a Comment