2016-06-17 3 views
1

私は文法でいくつかの愚かな間違いをしていますが、何が分かりませんか。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()) 
+1

それが動作します'== question.lower()'を使用している場合は? – syntonym

+0

質問自体を調べてみてください。 'print(QuestionsAndAnswers.query.filter(fun)/ question/Answers.question)== func.lower(question)))'と ' –

+0

また、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) 'は無効です(操作なし)。 –

答えて

1

問題は、MySQL関数LOWER()UPPER()cannot handle binary string typesだけがバイナリ文字列をそのまま返すということです。質問

question = Column(MEDIUMBLOB, nullable=False) 

の列として

は、バイナリ文字列型、

func.lower(QuestionsAndAnswers.question) 

が無操作されず、そのままバイナリ文字列を返す関数のアプリケーションです。これは、比較が "Hey"と "Hey"の間にあり、述部が一致しないことを意味します。

適切な修正プログラムは、このようなUnicodeUnicodeTextとして適切なテキストタイプを使用するモデル、テーブルやデータを変更することですが、迅速な解決には、キャストを追加することです:

from sqlalchemy import Unicode, cast 

func.lower(cast(QuestionsAndAnswers.question, Unicode)) 
関連する問題