【问题标题】:Postgresql and flask connectionPostgresql 和烧瓶连接
【发布时间】:2016-10-07 17:43:59
【问题描述】:

我在 python3 上使用烧瓶。

迁移脚本运行没有错误。主应用程序也运行良好。 我在运行 migrate 时使用 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()

【问题讨论】:

  • 您是如何进行迁移的?您确定您使用的应用程序知道您的模型吗?请edit your question 包含Minimal, Complete, and Verifiable example
  • 我包含了我的 manage.py 模块,是的,主应用程序知道它正在显示我手动输入的数据库中的数据
  • 您在上面发布的回溯中的错误是column user_info.password does not exist。似乎您的模型和实际数据库之间存在差异。

标签: postgresql flask mod-wsgi flask-sqlalchemy flask-migrate


【解决方案1】:

您说应该存在两张表,但我只看到在您的models.py 文件中为UserInfo 定义了一张。您是否在文件的其他位置也定义了UserPosts,或者您是否只创建了与它的关系?如果没有,您也应该为它创建一个模型。

另外需要指出的是,您应该使用db.relationship()db.ForeignKey(),而不是从sqlalchemy 导入ForeignKeyrelationship

我还可以看到您的网址缺少几个字符。你的 URL 应该看起来像 "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 以删除会话并使错误消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 2020-11-10
    • 2013-03-18
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多