私は文法でいくつかの愚かな間違いをしていますが、何が分かりませんか。SQLAlchemy func.lowerをMySQLで使用することができません
私は下の例でそれらの両方を回してDBフィールドと変数を比較するために、SQLAlchemyのfunc
を使用しようとしていますが、条件が満足されていない場合のように出力がヌルいます。
func
を使用せずに静的変数を渡しても同じことが起こります。
コード:
question = "Hey"
q1 = QuestionsAndAnswers.query.filter(func.lower(QuestionsAndAnswers.question) == func.lower(question)).all()
q2 = QuestionsAndAnswers.query.filter(QuestionsAndAnswers.question == "Hey").all()
print "q1", q1
print "q2", q2
出力:
q1 []
q2 [<intercom_bot.models.QuestionsAndAnswers object at 0x7f1e2c7add50>]
DB:
+----+-----------------+--------------------------------------------------+------------+
| id | question | answer | created_at |
+----+-----------------+--------------------------------------------------+------------+
| 1 | Hi | Hey, Here I am and here you are. How can I help? | NULL |
| 2 | Hello | Hey, Here I am and here you are. How can I help? | NULL |
| 3 | Helo | Hey, Here I am and here you are. How can I help? | NULL |
| 4 | Heelo | Hey, Here I am and here you are. How can I help? | NULL |
| 5 | Hell | Hey, Here I am and here you are. How can I help? | NULL |
| 6 | Hallo | Hey, Here I am and here you are. How can I help? | NULL |
| 7 | Hey | Hey, Here I am and here you are. How can I help? | NULL |
| 8 | He | Hey, Here I am and here you are. How can I help? | NULL |
| 9 | Ho | Hey, Here I am and here you are. How can I help? | NULL |
| 10 | I need help | Hey, Here I am and here you are. How can I help? | NULL |
| 11 | Help | Hey, Here I am and here you are. How can I help? | NULL |
| 12 | can you help me | Hey, Here I am and here you are. How can I help? | NULL |
| 13 | Greetings | Hey, Here I am and here you are. How can I help? | NULL |
+----+-----------------+--------------------------------------------------+------------+
PS:
print QuestionsAndAnswers.query.filter(func.lower(QuestionsAndAnswers.question) == func.lower(question))
はこれを示します:
SELECT questions_and_answers.id AS questions_and_answers_id, questions_and_answers.question AS questions_and_answers_question, questions_and_answers.answer AS questions_and_answers_answer, questions_and_answers.created_at AS questions_and_answers_created_at
FROM questions_and_answers
WHERE lower(questions_and_answers.question) = lower(:lower_1)
PPS:これはモデルです。
class QuestionsAndAnswers(Base):
"""docstring for QuestionsAndAnswers"""
__tablename__ = 'questions_and_answers'
id = Column(BIGINT, primary_key=True)
question = Column(MEDIUMBLOB, nullable=False)
answer = Column(MEDIUMBLOB, nullable=False)
created_at = Column(DATETIME, nullable=True,
default=datetime.datetime.now())
それが動作します'== question.lower()'を使用している場合は? – syntonym
質問自体を調べてみてください。 'print(QuestionsAndAnswers.query.filter(fun)/ question/Answers.question)== func.lower(question)))'と ' –
また、MySQLを使用していますか? 'question'属性はバイナリ文字列ですか?バイナリ文字列( 'BINARY'、' VARBINARY'、 'BLOB')に適用すると、' 'LOWER()' 'と' 'UPPER()' 'は無効です。"](http://dev.mysql.com/doc /refman/5.7/en/string-functions.html#function_lower)。少なくともあなたはPython 2を使っているので、 '' Hey ''はバイト列なので、' func.lower(question) 'は無効です(操作なし)。 –