2016-06-17 2 views
1

구문과 어리석은 실수를하고있다. 그러나 무엇을 알아낼 수 없다.SQLAlchemy func.lower를 MySQL과 함께 사용할 수 없다.

나는 낮은 경우에 둘으로 표시하여 DB 필드와 변수를 비교하는 SQLAlchemy의func을 사용하려고하지만, 조건이 만족되지 것처럼 출력은 널 (null) 제공됩니다.

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

이 그것을 사용할 수 있습니까 :

적절한 수정 모델, 테이블과 같은 UnicodeUnicodeText하지만 빠른 해결책이 캐스트를 추가하는 것 등의 적절한 텍스트 형식을 사용하는 데이터를 변경하는 것 '== question.lower()'를 사용한다면? – syntonym

+0

질의 자체를 검사 해보십시오 :'print (QuestionsAndAnswers.query.query.filter (fun.lower (QuestionsAndAnswers.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). 적어도 당신은 파이썬 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"사이에 있고 술어가 일치하지 않음을 의미합니다.

from sqlalchemy import Unicode, cast 

func.lower(cast(QuestionsAndAnswers.question, Unicode))