为什么这些字符串不会被 psycopg2 引用来进行查询?

发布于 2024-10-04 11:11:48 字数 1745 浏览 5 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除→记忆 2024-10-11 11:11:48

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 in WHERE UPPER("addr2zip"."street_name"::text) = UPPER(E'Common Ground'). Which version are you using?

小嗲 2024-10-11 11:11:48

要执行查询,请使用以下命令:

from django.db import DEFAULT_DB_ALIAS
queryset.query.get_compiler(DEFAULT_DB_ALIAS).as_sql()

To get the query to be executed, use something like:

from django.db import DEFAULT_DB_ALIAS
queryset.query.get_compiler(DEFAULT_DB_ALIAS).as_sql()
↙厌世 2024-10-11 11:11:48

但你只是自己粘贴它:

where = ['ctystate.zip5 = addr2zip.zip5 AND ctystate.city_name = %s' % 'Philipsburg'],

所以你得到了用语法错误构造的查询字符串:

AND ctystate.city_name = Philipsburg

But you just pasted it like that yourself:

where = ['ctystate.zip5 = addr2zip.zip5 AND ctystate.city_name = %s' % 'Philipsburg'],

so you got query string constructed with syntax error:

AND ctystate.city_name = Philipsburg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文