postgresql 烧瓶:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“用户”不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35047914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask : sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "users" does not exist
提问by user61629
I am working on a flask app based on http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982.
我正在开发基于http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982的烧瓶应用程序。
As part of the tut I'm trying to connect to a postgres server, with a structure as in the screenshot. I've added a db 'flask' which you can see.
作为 tut 的一部分,我正在尝试连接到 postgres 服务器,其结构如屏幕截图所示。我添加了一个你可以看到的数据库“烧瓶”。
Based on the tut I have the following code in my main file ('routes.py'):
基于 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', '[email protected]', 'admin1', '[email protected]')
guest = User('admi2', '[email protected]', 'admin', '[email protected]')
# guest = User('guest', '[email protected]')
db.session.add(admin)
db.session.add(guest)
db.session.commit()
models.py:
模型.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)
When run the debugger gives:
运行时,调试器给出:
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': '[email protected]', 'firstname': 'Admin', 'pwdhash': 'pbkdf2:sha1:1000$eZvJNKHOf59c34364e3d6094d126fa3ca2b327ab39e302', 'email': 'admin1'}]
What am I doing wrong?
我究竟做错了什么?
采纳答案by Ryan O'Donnell
You're initializing your database twice.
您正在初始化数据库两次。
I'd suggest taking a good look at this: http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/
我建议好好看看这个:http: //flask.pocoo.org/docs/0.10/patterns/sqlalchemy/
Essentially, you'll want to split things up into a few more files to prevent import issues and make things a little more clean. I've done the below which seems to work. Note, I've used SQLite, since I do not have Postgres installed on this box.
从本质上讲,您需要将内容拆分为更多文件,以防止出现导入问题并使内容更加整洁。我已经完成了以下似乎有效的操作。注意,我使用过 SQLite,因为我没有在这个盒子上安装 Postgres。
app.py
应用程序
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////test11.db'
models.py
模型.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
routes.py
路线.py
from models import User, db
db.create_all()
db.session.commit()
admin = User('admin', '[email protected]', 'admin1', '[email protected]')
guest = User('admi2', '[email protected]', 'admin', '[email protected]')
db.session.add(admin)
db.session.add(guest)
db.session.commit()
I'd definitely suggest looking over some tutorials! You'll need it: you should learn about web vulnerabilities, best practices, and so on.
我绝对建议查看一些教程!您将需要它:您应该了解 Web 漏洞、最佳实践等。
回答by Josh Kupershmidt
The tutorial you linked to has a section called "Create a User Model", under which it tells you how to use CREATE TABLE ...
to create your users
table. It gives some MySQL-specific syntax for this, although you are apparently using Postgres, so your command will be slightly different.
您链接到的教程有一个名为“创建用户模型”的部分,在该部分下,它会告诉您如何使用CREATE TABLE ...
创建users
表。它为此提供了一些特定于 MySQL 的语法,尽管您显然使用的是 Postgres,因此您的命令会略有不同。
But the screenshot you posted (from pgAdmin?) clearly shows that the "public" schema in the flask database has zero tables, so obviously you have not created this table yet. You'll have to create this table, you should be able to do so through pgAdmin, something like:
但是您发布的屏幕截图(来自 pgAdmin?)清楚地表明,flask 数据库中的“公共”模式有零个表,因此显然您还没有创建此表。您必须创建此表,您应该可以通过 pgAdmin 来创建,例如:
CREATE TABLE users (
uid serial PRIMARY KEY,
firstname varchar(100) not null,
... follow the guide for the rest of the columns ...
);