Python heroku上的Peewee和postgres,无法创建表
我尝试在没有数据库的情况下使用我的应用程序,但使用peewee和postgres,它根本无法创建表 我在网上尝试了不同的方法,但不起作用,如果你在heroku上使用过peewee,请帮助 我的模特Python heroku上的Peewee和postgres,无法创建表,python,postgresql,python-3.x,heroku,peewee,Python,Postgresql,Python 3.x,Heroku,Peewee,我尝试在没有数据库的情况下使用我的应用程序,但使用peewee和postgres,它根本无法创建表 我在网上尝试了不同的方法,但不起作用,如果你在heroku上使用过peewee,请帮助 我的模特 if os.environ.get('DATABASE_URL'): DATABASE_URL = os.environ.get('DATABASE_URL') db = urlparse(DATABASE_URL) user = db.username passwo
if os.environ.get('DATABASE_URL'):
DATABASE_URL = os.environ.get('DATABASE_URL')
db = urlparse(DATABASE_URL)
user = db.username
password = db.password
path = db.path[1:]
host = db.hostname
port = db.port
database = PostgresqlDatabase(path, user=user, password=password, host=host, port=port)
else:
database = PostgresqlDatabase('heroku')
class BaseModel(Model):
class Meta:
database = database
class User(BaseModel):
name = CharField()
email = CharField(null=False, unique=True)
将CREATETABLE从if语句移动到欢迎函数表后,将创建该表
@app.route('/')
def welcome():
call('printenv')
a = 'Default'
b = 'Default'
if os.environ.get('DATABASE_URL'):
a = os.environ.get('DATABASE_URL')
if os.environ.get('HEROKU'):
b = os.environ.get('HEROKU')
create_model_tables([Users], fail_silently=True)
Users.insert(name='John', email='Doe').execute()
return render_template('index.html', a=a, b=b)
if __name__ == '__main__':
app.run(port=5000, debug=True)
“用户”是保留表--请尝试:
使用这种方法,它很容易,而且出错的可能性很小
并且您必须在之前创建表,如果uuuu name_uuuu=='\uuuuu main:
,则此语句,因为在heroku上,您提供的设置使其下的任何内容都无法工作
像我的uswgi.ini
[uwsgi]
http-socket = :$(PORT)
master = true
die-on-term = true
module = app:app
memory-report = true
module=app:app
表示从app.py运行应用程序而不是运行app.py我已更改了表名,但仍然收到相同的错误。第1行:插入“用户”(“名称”、“电子邮件”)值('John'、'Doe')。。。2017-04-20T03:36:57.055897+00:00应用程序[web.1]:^2017-04-20T03:36:57.055896+00:00应用程序[web.1]:psycopg2.ProgrammingError:关系“用户”不存在显然,您需要创建表。
from playhouse.db_url import connect
import settings
local_url = 'postgresql://{}:{}@localhost:5432/{}'.format(settings.DB_USER, settings.DB_PASS, settings.DB_NAME)
database = connect(os.environ.get('DATABASE_URL') or local_url)
class BaseModel(Model):
class Meta:
database = database
[uwsgi]
http-socket = :$(PORT)
master = true
die-on-term = true
module = app:app
memory-report = true