0

我正在使用烧瓶python3

迁移脚本工作没有错误。主应用程序也运行良好。当我运行迁移时,我正在使用postgresql一切运行正常,但是当我进入postgresshell 时,我看不到任何正在创建的表。

模型.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()
4

1 回答 1

0

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

要指出的另一件事是,您应该使用and而不是导入ForeignKeyandrelationshipsqlalchemydb.relationship()db.ForeignKey()

我还可以看到您的网址缺少几个字符。而不是"postgresql:user:password@localhost/registration"你的 URL 应该看起来像"postgresql://user:password@localhost/registration"

最后,您是使用manage.py文件迁移和升级还是仅迁移?运行后,python manage.py db migrate您应该使用python manage.py db upgrade. 该migrate命令创建迁移脚本,而该upgrade命令执行它并将您的更改写入数据库。

最后要注意的一件事是,如果您在登录到服务器时更改user_info模型并在这些模型上运行迁移,有时会话中存储的内容与数据库中的内容之间会存在差异。这可能会导致应用程序崩溃,类似于您从 wsgi 发布的错误,让我相信这也可能发生在这里。确保在弄乱数据库之前注销,但如果确实发生这种情况,您应该能够清除 cookie 以删除会话并使错误消失。

于 2016-11-30T21:39:06.160 回答