首页 / PYTHON / python – Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“users”不存在
python – Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“users”不存在
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“users”不存在,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3358字,纯文字阅读大概需要5分钟。
内容图文
![python – Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“users”不存在](/upload/InfoBanner/zyjiaocheng/895/e2d02dd733154bc88465a4b2dba8d0cf.jpg)
我正在研究基于http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out–net-29982的烧瓶应用程序.
作为tut的一部分,我正在尝试连接到postgres服务器,其结构如截图所示.我添加了一个db’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()
models.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.
app.py
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////test11.db'
models.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
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”不存在全部内容,希望文章能够帮你解决python – Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“users”不存在所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。