Reject constant defaults on primary-key attributes

Đang mở Phù hợp với người mới
#1,712 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 5 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
84/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
mysql, python
Lĩnh vực
databases

Hướng nghiên cứu

Bắt đầu với compile_attribute trong declare.py và lần theo cách các giá trị mặc định của thuộc tính khóa chính được phân loại. Bổ sung kiểm thử cho giá trị mặc định hằng của khóa, giá trị mặc định CURRENT_TIMESTAMP của khóa và giá trị mặc định không thay đổi của thuộc tính phụ. Công việc được xem là hoàn tất khi giá trị mặc định hằng của khóa bị từ chối lúc khai báo bằng lỗi nêu tên thuộc tính và yêu cầu người dùng xóa giá trị mặc định, trong khi các giá trị mặc định được tạo tự động và của thuộc tính phụ vẫn được chấp nhận.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
Python
Star
198
Fork
103
Merge trung bình
5 ngày 1 giờ
Pull request đã merge (30 ngày)
12

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của datajoint/datajoint-python

Tất cả issue của datajoint/datajoint-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.