sqlalchemy.exc.ArgumentError:SQLAlchemy-“ POST”时出现烧瓶db错误?

时间:2020-08-07 17:57:19

标签: flask sqlalchemy flask-sqlalchemy

嗨,我已经从数据库查询中收到一条错误消息,我已经运行了它。 我的意思是,当我从HTML发送POST时,我收到消息:

sqlalchemy.exc.ArgumentError sqlalchemy.exc.ArgumentError:只能将'=','!=','is _()','isnot()','is_distinct_from()','isnot_distinct_from()'运算符与None / True / False < / p>

我的代码是:

app = Flask(__name__)

app.config['SECRET_KEY'] ='mypassword'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True


usr = "myuser:password"
app.config['SQLALCHEMY_DATABASE_URI']= "postgresql://" + usr + "@mydb"
db= SQLAlchemy(app)


def info_connect(value):
    if value==3:
        schema='MX_SL_CAR_SEM_QPX3_DLG'
    else:
        schema='MX_SL_SM_QPX4'
    return schema



info = db.Table('DLG_WeightCnv2', db.metadata,autoload=True, autoload_with=db.engine, schema=info_connect(value=1))
info2= db.Table('DLG_DownStream', db.metadata,autoload=True, autoload_with=db.engine, schema=info_connect(value=1))

@app.route('/index', methods=['POST', 'GET'])
def index():
    if request.method == 'POST':
        date= request.form.getlist('date')
        datestart = date[0]
        session['datestart']=datestart
        dateend =date[1]
        session['dateend']=dateend
             return redirect(url_for('results'))
return render_template("index.html")

@app.route('/results', methods=['POST', 'GET'])
def results():
    datestart=session.pop('datestart',None)
    dateend = session.pop('dateend',None)
    results = db.session.query(info).with_entities(info.c.RecipeName,info.c.LotNumber).filter(info.c.Timestamp < dateend).filter(info.c.Timestamp > datestart)
    output = []
    lotobj = {}
    output2 =[]
    for x in results:
        if x.LotNumber not in output:
            output.append(x.LotNumber)
            lotobj[x.RecipeName] = x.LotNumber
            if x.RecipeName not in output2:
                output2.append(x.RecipeName)
    if request.method == "POST":
        session['RecipeName'] = request.form.get('RecipeName')
        print(request.form.get('RecipeName'))
        return redirect(url_for('result'))
    return render_template("results.html", results=output, results2=output2)

当我打印列表和字典时,我可以看到结果...它们也填充了我的results.html, 我在results.html中发布提交时发生错误

<form  method="POST" action="/results">

    <select  method="post" action="/results" name="LotNumber" id="LotNumber" >
        {% for x in results %}
        <option value= "{{x}}" >{{x}}</option>"
        {% endfor %}
    </select>
    <select  method="POST" action="/results" name="RecipeName" id="RecipeName">
        {% for y in results2 %}
        <option value= "{{y}}" >{{y}}</option>"
        {% endfor %}
        <input type="submit">

    </select>

错误如下:

sqlalchemy.exc.ArgumentError sqlalchemy.exc.ArgumentError:只能将'=','!=','is _()','isnot()','is_distinct_from()','isnot_distinct_from()'运算符与None / True / False < / p>

回溯(最近通话最近) 调用中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py”,行1997 返回self.wsgi_app(环境,start_response) wsgi_app中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py”,行1985 响应= self.handle_exception(e) 在handle_exception中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py”,行1540 加价(exc_type,exc_value,tb) 重新列出文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask_compat.py”,第33行 提高价值 wsgi_app中的第1982行的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py” 响应= self.full_dispatch_request() 在full_dispatch_request中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py”,行1614 rv = self.handle_user_exception(e) 1517行中的handle_user_exception中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py” 加价(exc_type,exc_value,tb) 重新列出文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask_compat.py”,第33行 提高价值 在full_dispatch_request中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py”,行1612 rv = self.dispatch_request() 在dispatch_request中,文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ flask \ app.py”,行1598 返回self.view_functionsrule.endpoint 结果中的文件“ C:\ Users \ paulira002 \ PycharmProjects \ test \ test.py”,第99行 结果= db.session.query(info).with_entities(info.c.RecipeName,info.c.LotNumber).filter(info.c.Timestamp datestart) lt 中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ sqlalchemy \ sql \ operators.py”,第325行 返回self.operate(lt,other) 操作中的文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ sqlalchemy \ sql \ elements.py”

有人可以帮助我吗?非常感谢

1 个答案:

答案 0 :(得分:0)

想象一下,从这些行将datestartdateend中的任何一个设置为None

datestart = session.pop('datestart', None)
dateend = session.pop('dateend', None)

然后您执行数据库查询

results = (
    db
    .session
    .query(info)
    .with_entities(info.c.RecipeName,info.c.LotNumber)
    .filter(info.c.Timestamp < dateend)
    .filter(info.c.Timestamp > datestart)
)

您的实际过滤器看起来像info.c.Timestamp < Noneinfo.c.Timestamp > None

看到了吗?您正在检查Timestamp是否小于None并且大于None,这没有道理。

这就是为什么会出现错误的原因,因为您只能使用=!=is_()isnot()is_distinct_from()isnot_distinct_from()运算符None/True/False的值-根据错误消息。

更改这些行

datestart = session.pop('datestart', None)
dateend = session.pop('dateend', None)

datestart = session.pop('datestart', 0)
dateend = session.pop('dateend', 0)

您会没事的。