我已经阅读了20多个不同的问题,都有相同的问题 - 建议的答案没有解决我的问题。 我仍然收到sqlalchemy psycopg2.errors.InsufficientPrivilege: permission denied for relation <<table>>
错误消息。
环境:EC2,debian 8,postgresql,flask,sqlalchemy 我在postgresql中的表:
Create table Members(
id BIGSERIAL PRIMARY KEY,
joinDate TIMESTAMPTZ DEFAULT Now(),
password TEXT NOT NULL
);
在PostgreSQL中直接执行:INSERT INTO members (password) VALUES('123') RETURNING id;
可以完美地运行。
我已经授予我的用户所有可能的授权。
grant all privileges on database <my_db> to <my_user>
grant all privileges on all table in schema public to <my_user>
grant all privileges on all relations in schema public to <my_user>
我还创建了一个.htaccess
文件,放在var/www/html
下面,并添加了Header set Access-Control-Allow-Origin "*"
我的表与sqlalchemy的member.py
进行了映射
from datetime import datetime
from sqlalchemy import create_engine, Column, String, Integer, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from marshmallow import Schema, fields
from sqlalchemy.schema import UniqueConstraint
import sqlalchemy as sa
Engine = create_engine('postgresql://<my_user>:<my_psw>@<my_db_url>/<my_dn_name>')
Session = sessionmaker(bind=Engine)
Base = declarative_base()
class MemberSchema(Schema):
id = fields.Number()
password = fields.Str()
class Member(Base):
__tablename__ = 'members'
id = Column(Integer, primary_key=True)
password = Column(String)
def __init__(self, password):
self.password = password
处理POST请求并将其插入 test.py
中的代码:
Processing the POST request and inserting it into test.py
in code:
from flask import Flask, render_template, request,jsonify
from flask_cors import CORS, cross_origin
import logging
def register_member(password):
session = Session()
newMember = Member(password)
session.add(newMember)
session.commit()
return newMember
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route("/register", methods=["GET", "POST"])
@cross_origin()
def register():
try:
formData = request.form
register_member(formData['password'])
return jsonify({"message":"Regsitrated successfully!"})
except Exception as ex:
logging.debug(ex)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=1234, debug=True)
执行此代码会导致异常,出现以下错误:
DEBUG:root:(psycopg2.errors.InsufficientPrivilege) permission denied for relation members
[SQL: INSERT INTO members (password) VALUES (%(password)s) RETURNING members.id]
[parameters: {'password': '4444'}]
和
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
希望能得到任何帮助,感激不尽。