烧瓶Postgres DB连接问题init_db返回错误的路径 我有一个Flask + PostgreSQL项目,用于/ 现在函数init_db返回错误的路径,尽管
app.config.get('SQLALCHEMY_DATABASE_URI')
和 SQLALCHEMY_DATABASE_URI环境变量包含正确的路径。 init_db将用户名替换为***
有人可以帮我吗?我发疯了,坚持了一段时间。
#!flask/bin/python
from migrate.versioning import api
#from config import SQLALCHEMY_MIGRATE_REPO
#from config import SQLALCHEMY_DATABASE_URI
import config
from flask import current_app
import os.path
########## usually called in run.py ################
from app import create_app, db
from app.models import User, Student, Teacher, Profile, Strength, Weaknesse, Role
app = create_app()
########## usually called in run.py ################
SQLALCHEMY_DATABASE_URI = app.config.get('SQLALCHEMY_DATABASE_URI')
SQLALCHEMY_MIGRATE_REPO = app.config.get('SQLALCHEMY_MIGRATE_REPO')
#do everything from https://www.mbeckler.org/blog/?p=218
from sqlalchemy import *
from sqlalchemy.schema import *
from sqlalchemy.engine import reflection
#do everything from https://www.mbeckler.org/blog/?p=218
#from flask.ext.sqlalchemy import SQLAlchemy
#from https://github.com/numpy/numpy/issues/7556
#import collections
#import numpy
#FROM https://www.mbeckler.org/blog/?p=218
from sqlalchemy.engine import reflection
from sqlalchemy.schema import (
MetaData,
Table,
DropTable,
ForeignKeyConstraint,
DropConstraint,
)
def db_DropEverything(app, db):
# From http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DropEverything
with app.app_context():
conn=db.engine.connect()
# the transaction only applies if the DB supports
# transactional DDL, i.e. Postgresql, MS SQL Server
trans = conn.begin()
inspector = reflection.Inspector.from_engine(db.engine)
# gather all data first before dropping anything.
# some DBs lock after things have been dropped in
# a transaction.
metadata = MetaData()
tbs = []
all_fks = []
for table_name in inspector.get_table_names():
fks = []
for fk in inspector.get_foreign_keys(table_name):
if not fk['name']:
continue
fks.append(
ForeignKeyConstraint((),(),name=fk['name'])
)
t = Table(table_name,metadata,*fks)
tbs.append(t)
all_fks.extend(fks)
for fkc in all_fks:
conn.execute(DropConstraint(fkc))
for table in tbs:
conn.execute(DropTable(table))
trans.commit()
#FROM https://www.mbeckler.org/blog/?p=218
db.reflect()
db.drop_all()
db.create_all()
#db.session.commit() Check if changes are actually made even without this cmd
db_DropEverything(app, db)
'''
db.reflect()
db.drop_all()
db.create_all()
'''
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
更多代码
#menta/config.py file
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'postgresql://postgres:tomererezor1@localhost:5432/menta'
#SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:tomererezor1@localhost:5432/menta' #menta db
print("SQLALCHEMY_DATABASE_URI: ", SQLALCHEMY_DATABASE_URI)
SQLALCHEMY_TRACK_MODIFICATIONS = True
#app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
WTF_CSRF_ENABLED = True
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
print("SQLALCHEMY_MIGRATE_REPO IS : ", SQLALCHEMY_MIGRATE_REPO)
TEMPLATES_AUTO_RELOAD = True
DEBUG = True
下一个文件: init .py
#FROM menta/app/__init__.py file
import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
import os
from flask import Flask, request, current_app
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_bootstrap import Bootstrap
from config import Config
from wtforms import *
from flask_migrate import Migrate
#from https://stackoverflow.com/questions/43466927/sqlalchemy-database-uri-not-set
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:tomererezor1@localhost:5432/menta'
#from https://stackoverflow.com/questions/43466927/sqlalchemy-database-uri-not-set
db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = 'users.login'
login.login_message = 'Please log in to access this page.'
bootstrap = Bootstrap()
'''
#debug
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db.init_app(app)
print("IIIIIIIIIIIIIIIIIIIIIn create_app app.config['SQLALCHEMY_DATABASE_URI']: ", app.config['SQLALCHEMY_DATABASE_URI'], db)
import pdb; pdb.set_trace()
#debug
'''
#FROM https://github.com/miguelgrinberg/microblog/blob/v0.15/app/__init__.py
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
db.init_app(app)
#debug
print("app BBBBBBBBBBBBBefore create_app app.config['SQLALCHEMY_DATABASE_URI']: ", app.config['SQLALCHEMY_DATABASE_URI'], db)
db.init_app(app)
print("app AAAAAAAAAAAAfter create_app current_app.config['SQLALCHEMY_DATABASE_URI']: ", app.config['SQLALCHEMY_DATABASE_URI'], db)
print('\n')
with app.app_context():
print("current_app BWFFFFFFFFFFFFFFFFFFore create_app current_app.config['SQLALCHEMY_DATABASE_URI']: ", current_app.config['SQLALCHEMY_DATABASE_URI'], db)
db.init_app(app)
print("current_app AAAAAAAAAAAAfter create_app current_app.config['SQLALCHEMY_DATABASE_URI']: ", current_app.config['SQLALCHEMY_DATABASE_URI'], db)
print("\n\n\n\n")
import pdb; pdb.set_trace()
#debug
#debug
import re
print("config in create_app")
print(re.sub("[,]" , "\\n\\n********************" , str(app.config)))
#debug
migrate.init_app(app, db)
login.init_app(app)
bootstrap.init_app(app)
from app.users.users import usr
from app.schools.schools import scl
from app.students.students import std
from app.teachers.teachers import tchr
from app.profile.profile import prf
from app.weaknesses.weaknesses import wkns
from app.strengthes.strengthes import strn
from app.select.select import slct
from app.destinations.destinations import dst
from app.goals.goals import goal
from app.resources.resources import rsrc
# register our blueprints
app.register_blueprint(usr)
app.register_blueprint(scl)
app.register_blueprint(std)
app.register_blueprint(tchr)
app.register_blueprint(prf)
app.register_blueprint(strn)
app.register_blueprint(wkns)
app.register_blueprint(slct)
app.register_blueprint(dst)
app.register_blueprint(goal)
app.register_blueprint(rsrc)
return app
from app import models
from app.models import *
from app import *
'''
#debug
app = create_app()
print("IIIIIIIIIIIIIIIIIIIIIn init current_app.config['SQLALCHEMY_DATABASE_URI']: ", app.config['SQLALCHEMY_DATABASE_URI'])
with app.app_context():
print("IIIIIIIIIIIIIIIIIIIIIn init current_app.config['SQLALCHEMY_DATABASE_URI']: ", current_app.config['SQLALCHEMY_DATABASE_URI'])
#debug
'''