gpt4 book ai didi

python - Flask : sqlalchemy. exc.ProgrammingError : (psycopg2. ProgrammingError)关系 "users"不存在

转载 作者:行者123 更新时间:2023-11-29 11:31:23 24 4
gpt4 key购买 nike

enter image description here

我正在开发一个基于 http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982 的 flask 应用程序.

作为 tut 的一部分,我正在尝试连接到 postgres 服务器,其结构如屏幕截图所示。我添加了一个您可以看到的数据库“ flask ”。

基于 tut,我的主文件('routes.py')中有以下代码:

from flask.ext.sqlalchemy import SQLAlchemy

from flask import Flask

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:123@localhost/flask"

db = SQLAlchemy(app)
from models import User
# db.init_app(app)
db.create_all()
db.session.commit()


admin = User('admin', 'admin@example.com', 'admin1', 'admin1@example.com')
guest = User('admi2', 'admin@ex1ample.com', 'admin', 'admin2@example.com')
# guest = User('guest', 'guest@example.com')
db.session.add(admin)
db.session.add(guest)
db.session.commit()

模型.py:

from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash

db = SQLAlchemy()

class User(db.Model):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key = True)
firstname = db.Column(db.String(100))
lastname = db.Column(db.String(100))
email = db.Column(db.String(120), unique=True)
pwdhash = db.Column(db.String(54))

def __init__(self, firstname, lastname, email, password):
self.firstname = firstname.title()
self.lastname = lastname.title()
self.email = email.lower()
self.set_password(password)

def set_password(self, password):
self.pwdhash = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.pwdhash, password)

当运行调试器时给出:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "users" does not exist
LINE 1: INSERT INTO users (firstname, lastname, email, pwdhash) VALU...
^
[SQL: 'INSERT INTO users (firstname, lastname, email, pwdhash) VALUES (%(firstname)s, %(lastname)s, %(email)s, %(pwdhash)s) RETURNING users.uid'] [parameters: {'lastname': 'Admin@Example.Com', 'firstname': 'Admin', 'pwdhash': 'pbkdf2:sha1:1000$eZvJNKHO$64f59c34364e3d6094d126fa3ca2b327ab39e302', 'email': 'admin1'}]

我做错了什么?

最佳答案

你正在初始化你的数据库两次。

我建议好好看看这个:http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/

从本质上讲,您需要将文件拆分成更多的文件,以防止出现导入问题并使文件更加整洁。我已经完成了以下似乎有效的操作。请注意,我使用的是 SQLite,因为我没有在此机器上安装 Postgres。

应用.py

from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////test11.db'

模型.py

from flask.ext.sqlalchemy import SQLAlchemy
from app import app
db = SQLAlchemy(app)

class User(db.Model):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key = True)
firstname = db.Column(db.String(100))
lastname = db.Column(db.String(100))
email = db.Column(db.String(120), unique=True)
pwdhash = db.Column(db.String(54))

def __init__(self, firstname, lastname, email, password):
self.firstname = firstname.title()
self.lastname = lastname.title()
self.email = email.lower()
self.set_password(password)

def set_password(self, password):
self.pwdhash = (password)

def check_password(self, password):
return password

路线.py

from models import User, db

db.create_all()
db.session.commit()

admin = User('admin', 'admin@example.com', 'admin1', 'admin1@example.com')
guest = User('admi2', 'admin@ex1ample.com', 'admin', 'admin2@example.com')
db.session.add(admin)
db.session.add(guest)
db.session.commit()

我绝对建议您查看一些教程!您将需要它:您应该了解 Web 漏洞、最佳实践等。

关于python - Flask : sqlalchemy. exc.ProgrammingError : (psycopg2. ProgrammingError)关系 "users"不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com