我在python3
上使用了烧瓶。
迁移脚本没有错误。主应用程序运行正常。
我在运行迁移时使用postgresql
一切运行正常但是当我进入postgres
shell时,我看不到任何正在创建的表。
modesl.py
from imports import db
from datetime import datetime
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
class UserInfo(db.Model):
__tablename_ = 'user_info'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(80), unique=True)
password = db.Column(db.String(100), nullable=False)
posts = relationship('UserPosts', backref='posts')
def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password
def __repr__(self):
return '{}-{}'.format(self.username, self.email)
正如你所看到我在那里有两个表,但我只是在我的数据库中看到user_info
,因为我手动创建了它,但另一个表我看不到。
export DATABASE_URL="postgresql:user:password@localhost/registration"
tail /var/log/apache2/error.log
[Fri Oct 07 17:21:56.049888 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 185, in reraise, referer: http://localhost/login/
[Fri Oct 07 17:21:56.049900 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] raise value.with_traceback(tb), referer: http://localhost/login/
[Fri Oct 07 17:21:56.049919 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context, referer: http://localhost/login/
[Fri Oct 07 17:21:56.049931 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] context), referer: http://localhost/login/
[Fri Oct 07 17:21:56.049951 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 450, in do_execute, referer: http://localhost/login/
[Fri Oct 07 17:21:56.049962 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] cursor.execute(statement, parameters), referer: http://localhost/login/
[Fri Oct 07 17:21:56.050034 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column user_info.password does not exist, referer: http://localhost/login/
[Fri Oct 07 17:21:56.050088 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] LINE 1: ...nfo_username, user_info.email AS user_info_email, user_info...., referer: http://localhost/login/
[Fri Oct 07 17:21:56.050106 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] ^, referer: http://localhost/login/
[Fri Oct 07 17:21:56.050122 2016] [wsgi:error] [pid 19131:tid 140331236435712] [client 174.58.31.189:34358] [SQL: 'SELECT user_info.id AS user_info_id, user_info.username AS user_info_username, user_info.email AS user_info_email, user_info.password AS user_info_password \\nFROM user_info'], referer: http://localhost/login/
我不知道我是否以正确的方式设置了与数据库的连接,或者根本没有创建任何内容。
感谢。
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
import os
import __init__
__init__.app.config.from_object(os.environ['APP_SETTINGS'])
migrate = Migrate(__init__.app, __init__.db)
manager = Manager(__init__.app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
答案 0 :(得分:0)
您说有两个表应该存在,但我只在models.py
文件中看到UserInfo
中有一个表。您是否在文件的其他位置定义了UserPosts
,或者您是否只创建了与它的关系?如果没有,你也应该为它创建一个模型。
需要指出的另一点是,您应该使用ForeignKey
和relationship
sqlalchemy
导入db.relationship()
和db.ForeignKey()
。
我还可以看到您的网址缺少几个字符。您的网址应该"postgresql:user:password@localhost/registration"
"postgresql://user:password@localhost/registration"
最后,您是使用manage.py
文件进行迁移和升级还是仅迁移?运行python manage.py db migrate
后,您应该使用python manage.py db upgrade
关注它。 migrate
命令创建迁移脚本,而upgrade
命令执行它并将更改写入数据库。
最后要注意的一点是,如果您在登录服务器时更改user_info
模型并对其进行迁移,有时会话中存储的内容与数据库中的内容之间会存在差异。这可能会导致应用程序崩溃,类似于您从wsgi发布的错误,这让我相信这可能也发生在这里。确保在弄乱数据库之前注销,但如果确实发生了这种情况,您应该能够清除cookie以删除会话并使错误消失。
答案 1 :(得分:0)
这将使用SQLAlchemy连接postgres。 请参阅http://flask-sqlalchemy.pocoo.org/2.3/config/以获取配置密钥
from flask import Flask, render_template, redirect, url_for, request
from flask_sqlalchemy import SQLAlchemy
import os
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost/databasename'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # silence the deprecation warning
db = SQLAlchemy(app)