我有以下 Python 代码
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://my_user_name:@localhost:5432/my_db'
db = SQLAlchemy(app)
@app.route("/submit_signup_email", methods=["POST"])
def submit_signup_email():
email = request.form["email"]
created_timestamp = int(time.time())
signup = Signup(email, created_timestamp)
db.session.add(signup)
db.session.commit()
# not working...
test = Signup.query.filter_by(email=email).first()
哪个工作正常,我可以检查一行是否已正确插入到我的数据库中。
但是如果我在“db.session.commit()”之后运行这段代码.....
test = Signup.query.filter_by(email=email).first()
我收到以下错误。
OperationalError:(OperationalError)无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受Unix域套接字“/var/pgsql_socket/.s.PGSQL.5432”上的连接?
我不确定问题是什么,我的注册模型在“models.py”中看起来像这样
from flask_sqlalchemy import SQLAlchemy
from app import app, db
class Signup(db.Model):
__tablename__ = "signups"
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String)
created_timestamp = db.Column(db.Integer)
def __init__(self, email, created_timestamp):
self.email = email
self.created_timestamp = created_timestamp
def __repr__(self):
return self.email