Reject constant defaults on primary-key attributes

未关闭 适合新手
#1,712 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 5 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

从 compile_attribute 入手,在 declare.py 中追踪主键属性默认值的分类方式。补充测试,覆盖常量键默认值、CURRENT_TIMESTAMP 键默认值,以及保持不变的次要属性默认值。完成标准是:常量键默认值在声明时被拒绝,并报错指出属性名称且告知用户移除默认值,同时生成的默认值和次要属性默认值仍被接受。

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

描述

bug

Problem

The definition language accepts a constant default on a primary-key attribute. compile_attribute in declare.py rejects only nullable key attributes; any other default above the --- separator compiles to NOT NULL DEFAULT <value>.

A constant default lets an insertion omit part of an entity's identity. The database then fills in the same value for every such insertion: the first one succeeds silently with the default key, and the next one collides.

Reproduction

DataJoint 2.3.5.dev, MySQL 8.0:

@schema
class Probe(dj.Manual):
    definition = """
    probe_id = 1 : int32
    ---
    label : varchar(16)
    """

Probe.insert1({"label": "x"})  # accepted, no key supplied
Probe.to_dicts()               # [{'probe_id': 1, 'label': 'x'}]

Proposed rule

  • Reject constant defaults on primary-key attributes at declaration, with an error naming the attribute.
  • Keep allowing generated defaults, values produced afresh at each insertion. CURRENT_TIMESTAMP is the one the definition language accepts today, and visit_ts = CURRENT_TIMESTAMP : datetime in the key works as a generated key. A generated UUID default would fall under the same rule if it is ever supported.

Generated keys remain surrogates, with the costs the docs already describe in Explicit keys, no auto-increment: a repeated insertion makes a second entity instead of a key collision. They are allowed but not recommended.

This matches the rule in the Relational Workflow Model specification: key attributes are never null and take no constant default; a nullable attribute's only default is null; an attribute without a default must be supplied.

Scope

  • The check runs at declaration time only, so existing tables are unaffected. A schema that declares a key attribute with a constant default will fail to declare after the change, which is the intent; the error message should say to remove the default.
  • Foreign-key attributes in the key are unaffected, since dependencies take no defaults.

Tests

  • A declaration with k = 1 : int32 above the separator raises.
  • A declaration with ts = CURRENT_TIMESTAMP : datetime above the separator succeeds.
  • Defaults on secondary attributes are unchanged.
主要语言
Python
星标
198
派生
103
平均合并
5 天 1 小时
30 天内合并 PR
12

环境准备

  • 提供 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 摘要。