Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

JSON path equality against a non-string value: silently empty on MySQL, raises on PostgreSQL

未关闭
#1,564 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
mysql, postgresql, python
领域
databases

调研方向

Start in condition.py around prep_value at line 336, then trace adapter.json_path_expr for the MySQL and PostgreSQL implementations. Reproduce the listed restrictions against both backends and add regression coverage for string, integer, and boolean values. Done means JSON-path comparisons behave consistently across backends and never silently return wrong rows.

由索引模型根据 Issue 内容生成。

描述

bug

Table & {"json_attr.field": value} only behaves correctly when value is a string. A Python bool returns the wrong answer on MySQL and raises on PostgreSQL; a Python int raises on PostgreSQL.

Sibling of #1563 — same root (JSON extraction yields text), different side: that one is the :type annotation on projection, this one is the Python value on restriction.

Repro

@schema
class E(dj.Manual):
    definition = """
    name : varchar(32)
    ---
    s : json
    """
E.insert([
    {"name": "a", "s": {"vendor": "Acme",  "ch": 64, "cal": True}},
    {"name": "b", "s": {"vendor": "Other", "ch": 16, "cal": False}},
])

Each row should select ['a']:

restriction MySQL 8.0 postgres:15
& {"s.vendor": "Acme"} ['a'] ['a']
& {"s.ch": 64} ['a'] UndefinedFunction: operator does not exist: text = integer
& {"s.ch": "64"} ['a'] ['a']
& {"s.cal": True} [] UndefinedFunction: operator does not exist: text = boolean
& {"s.cal": "true"} ['a'] ['a']

The MySQL boolean row is the serious one: no error, no rows, and the natural reading of an empty result is "nothing is calibrated."

Cause

adapter.json_path_expr yields json_value(...) / jsonb_extract_path_text(...), both of which return text. prep_value (condition.py:336) then renders the Python value by its own type, so the comparison becomes text = <non-text>:

  • PostgreSQL refuses the comparison outright.
  • MySQL coerces for numerics, which is why 64 happens to work, but compares the extracted true against 1 for a bool and matches nothing.

The asymmetry is invisible to a user: the same expression is correct, wrong, or an error depending on the value's Python type and the backend.

Suggested fix

On the JSON-path branch of prep_value, render the comparison value as text — or cast the extraction to the value's type — so that True, 64 and "Acme" all behave the same way on both backends.

Whichever way, a bool must not silently match nothing. If a given comparison cannot be made portable, raising is acceptable; returning the wrong rows is not.

Documentation

This is almost certainly why tutorials/advanced/json-type.ipynb teaches "Filtering on JSON Content — fetch then filter in Python" and lists "Filter in Python" as an inherent property of JSON in its Design Guidelines. Server-side filtering does work, with the value passed as a string; the tutorial's advice reads as a limitation of the type rather than of this behavior. Worth revisiting together.

主要语言
Python
星标
197
派生
98
平均合并
1 天 23 小时
30 天内合并 PR
6

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

datajoint/datajoint-python 的其他 Issue

查看 datajoint/datajoint-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。