为什么这些字符串不会被 psycopg2 引用来进行查询?
使用 psycopg2 引擎用 django 编写应用程序。它似乎并不总是想引用我的字符串。这是一个测试用例:
>>> from pypvs.search.models import Addr2zip
>>> kwargs = {
... 'street_name__iexact': 'Common Ground',
... 'state_id__iexact': 'MT',
... }
>>> addrMatch = Addr2zip.objects.extra(
... where = ['ctystate.zip5 = addr2zip.zip5 AND ctystate.city_name = \'%s\'' % 'Philipsburg'],
... tables = ['ctystate', 'addr2zip']
... ).filter(**kwargs).order_by('zip5', 'street_name', 'primary_address_low', 'secondary_address_low')
>>> print addrMatch.query
SELECT "addr2zip"."addr2zip_id", "addr2zip"."zip5", "addr2zip"."zip4_low", "addr2zip"."zip4_high", "addr2zip"."street_direction", "addr2zip"."street_name", "addr2zip"."street_suffix", "addr2zip"."street_post_direction", "addr2zip"."primary_address_low", "addr2zip"."primary_address_high", "addr2zip"."primary_address_parity", "addr2zip"."secondary_address", "addr2zip"."secondary_address_low", "addr2zip"."secondary_address_high", "addr2zip"."secondary_address_parity", "addr2zip"."state_id", "addr2zip"."county_code", "addr2zip"."municipality_key", "addr2zip"."urbanization_key", "addr2zip"."record_type" FROM "addr2zip" , "ctystate" WHERE (ctystate.zip5 = addr2zip.zip5 AND ctystate.city_name = Philipsburg AND UPPER("addr2zip"."state_id"::text) = UPPER(MT) AND UPPER("addr2zip"."street_name"::text) = UPPER(Common Ground) ) ORDER BY "addr2zip"."zip5" ASC, "addr2zip"."street_name" ASC, "addr2zip"."primary_address_low" ASC, "addr2zip"."secondary_address_low" ASC
这些字符串没有被引用的原因可能是什么?例如,“Common Ground”:
AND UPPER("addr2zip"."street_name"::text) = UPPER(Common Ground)
不确定问题是否出在我的实现、psycopg2 或 django ORM 中。我将不胜感激任何想法。
Writing an app with django, using the psycopg2 engine. It doesn't always seem to want to quote my strings. Here is a test case:
>>> from pypvs.search.models import Addr2zip
>>> kwargs = {
... 'street_name__iexact': 'Common Ground',
... 'state_id__iexact': 'MT',
... }
>>> addrMatch = Addr2zip.objects.extra(
... where = ['ctystate.zip5 = addr2zip.zip5 AND ctystate.city_name = \'%s\'' % 'Philipsburg'],
... tables = ['ctystate', 'addr2zip']
... ).filter(**kwargs).order_by('zip5', 'street_name', 'primary_address_low', 'secondary_address_low')
>>> print addrMatch.query
SELECT "addr2zip"."addr2zip_id", "addr2zip"."zip5", "addr2zip"."zip4_low", "addr2zip"."zip4_high", "addr2zip"."street_direction", "addr2zip"."street_name", "addr2zip"."street_suffix", "addr2zip"."street_post_direction", "addr2zip"."primary_address_low", "addr2zip"."primary_address_high", "addr2zip"."primary_address_parity", "addr2zip"."secondary_address", "addr2zip"."secondary_address_low", "addr2zip"."secondary_address_high", "addr2zip"."secondary_address_parity", "addr2zip"."state_id", "addr2zip"."county_code", "addr2zip"."municipality_key", "addr2zip"."urbanization_key", "addr2zip"."record_type" FROM "addr2zip" , "ctystate" WHERE (ctystate.zip5 = addr2zip.zip5 AND ctystate.city_name = Philipsburg AND UPPER("addr2zip"."state_id"::text) = UPPER(MT) AND UPPER("addr2zip"."street_name"::text) = UPPER(Common Ground) ) ORDER BY "addr2zip"."zip5" ASC, "addr2zip"."street_name" ASC, "addr2zip"."primary_address_low" ASC, "addr2zip"."secondary_address_low" ASC
What could be the reason that these strings are not quoted? For instance, 'Common Ground':
AND UPPER("addr2zip"."street_name"::text) = UPPER(Common Ground)
Not sure if the problem is in my implementation, psycopg2, or the django ORM. I'd appreciate any ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
str(query)
仅返回查询的近似表示。您是否想将其传递到数据库?使用
iexact
发出的查询在 Django 1.2.3 中似乎是正确的。上面的结果将是WHERE UPPER("addr2zip"."street_name"::text) = UPPER(E'Common Ground')
。您使用哪个版本?str(query)
only returns an approximative representation of the query. Are you trying to pass it to the database?The query issued with
iexact
seems correct with Django 1.2.3. The above would result inWHERE UPPER("addr2zip"."street_name"::text) = UPPER(E'Common Ground')
. Which version are you using?要执行查询,请使用以下命令:
To get the query to be executed, use something like:
但你只是自己粘贴它:
所以你得到了用语法错误构造的查询字符串:
But you just pasted it like that yourself:
so you got query string constructed with syntax error: