Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)relation“users”不存在 [英] Flask : sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "users" does not exist

查看:3496
本文介绍了Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)relation“users”不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发基于 http://code.tutsplus.com/tutorials/intro-to-flask-签约入和出 - 网29982

作为tut的一部分,我试图连接到一个postgres服务器,其结构如截图所示。我已经添加了一个数据库'瓶',你可以看到。



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

<$从flask.ext.sqlalchemy导入SQLAlchemy

从烧瓶导入烧瓶

app =烧瓶(__ name__)

app.config ['SQLALCHEMY_DATABASE_URI'] =postgresql:// postgres:123 @ localhost / flask
$ b $ db = SQLAlchemy(app)
from models import User
#db.init_app(app)
db.create_all()
db.session.commit()

$ b 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:

来自flask.ext.sqlalchemy的

 从werkzeug导入SQLAlchemy 
导入generate_password_hash,check_password_hash

db = SQLAlchemy()
$ b $ 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)
$ b $ def check_password(self,password):
return check_password_hash(self.pwdhash,password )

运行调试器时会给出:

 sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系用户不存在
行1:插入用户(firstname,l (名,电子邮件,pwdhash)VALU ...
^
[SQL:INSERT INTO用户(名,姓,电子邮件,pwdhash)VALUES(%(firstname)s,%(lastname)s,% (email)s,%(pwdhash)s)RETURNING users.uid'] [参数:{'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)
$ b $ 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(密码)

def set_password(s elf,密码):
self.pwdhash =(密码)

def check_password(self,password):
返回密码

routes.py

  from models import User,db 

db.create_all()
db.session.commit()
$ b admin = User('admin','admin@example.com','admin1 ','admin1@example.com')
guest =用户('admi2','admin@ex1ample.com','admin','admin2@example.com')
db.session。添加(admin)
db.session.add(guest)
db.session.commit()

我肯定会建议看一些教程!您将需要它:您应该了解网络漏洞,最佳实践等等。

I am working on a flask app based on 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.

Based on the tut I have the following code in my main file ('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)

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': 'Admin@Example.Com', 'firstname': 'Admin', 'pwdhash': 'pbkdf2:sha1:1000$eZvJNKHO$64f59c34364e3d6094d126fa3ca2b327ab39e302', 'email': 'admin1'}]

What am I doing wrong?

解决方案

You're initializing your database twice.

I'd suggest taking a good look at this: 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.

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()

I'd definitely suggest looking over some tutorials! You'll need it: you should learn about web vulnerabilities, best practices, and so on.

这篇关于Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)relation“users”不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆