0
votes

Hi I'm recieving an error message from my db query, after I have already run it... I mean when I'm sending the POST from the HTML I'm getting the message:

sqlalchemy.exc.ArgumentError sqlalchemy.exc.ArgumentError: Only '=', '!=', 'is_()', 'isnot()', 'is_distinct_from()', 'isnot_distinct_from()' operators can be used with None/True/False

My code is:

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)

when I print the lists and the dict I can see the results... they also populate my results.html, the error occur when I post submit in 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>

the error is the following:

sqlalchemy.exc.ArgumentError sqlalchemy.exc.ArgumentError: Only '=', '!=', 'is_()', 'isnot()', 'is_distinct_from()', 'isnot_distinct_from()' operators can be used with None/True/False

Traceback (most recent call last) File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1997, in call return self.wsgi_app(environ, start_response) File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1985, in wsgi_app response = self.handle_exception(e) File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1540, in handle_exception reraise(exc_type, exc_value, tb) File "C:\ProgramData\Anaconda3\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\ProgramData\Anaconda3\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "C:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint File "C:\Users\paulira002\PycharmProjects\test\test.py", line 99, in results results = db.session.query(info).with_entities(info.c.RecipeName,info.c.LotNumber).filter(info.c.Timestamp < dateend).filter(info.c.Timestamp > datestart) File "C:\ProgramData\Anaconda3\lib\site-packages\sqlalchemy\sql\operators.py", line 325, in lt return self.operate(lt, other) File "C:\ProgramData\Anaconda3\lib\site-packages\sqlalchemy\sql\elements.py", line 692, in operate

Anyone can help me? Thanks a lot

1

1 Answers

0
votes

Imagine when either of your datestart or dateend is set to None from these lines

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

Then you execute your database query

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

Your actual filter would look like, info.c.Timestamp < None and info.c.Timestamp > None.

See? You are checking if Timestamp is less than to None and is greater than None, which doesn't make sense.

That's why you get an error because you can only use =, !=, is_(), isnot(), is_distinct_from() or isnot_distinct_from() operators for None/True/False values — as per the error message.

Change these lines

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

to

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

and you'll be fine.