Two foreign keys that bring the same attribute name with different lineages are merged into one column

Đang mở
#1,748 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ó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
72/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

Start in src/datajoint/declare.py around lines 487-488 (where the second namesake attribute is silently skipped) and 512-516 (where the lineage map is overwritten by the last foreign key). Read how the heading and lineage map are built while foreign keys are processed, then reproduce with the Mouse/Probe/Implant example on MySQL. Done means a DataJointError naming the attribute and both lineages is raised for differing lineages, the homologous Recording/subject_id merge still works, the renamed workaround is tested, and the rule is documented in table-declaration.md.

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

Mô tả

bug

Summary

When two foreign keys of one table bring attributes with the same name but different lineages, the declaration is accepted, and the two attributes become one shared column. The column then carries both foreign key constraints, and ~lineage records only the lineage of the foreign key declared last.

Merging namesakes is correct when they are homologous: two parents that both inherit subject_id from Subject should share the column. It is wrong when the namesakes come from unrelated parents.

Reproduction (master @ 207068d, MySQL 8.0)

@schema
class Mouse(dj.Manual):
    definition = """
    id : int32
    ---
    mouse_name : varchar(32)
    """

@schema
class Probe(dj.Manual):
    definition = """
    id : int32
    ---
    probe_serial : varchar(32)
    """

@schema
class Implant(dj.Manual):      # expected: DataJointError; actual: accepted
    definition = """
    -> Mouse
    -> Probe
    ---
    implant_date : date
    """

Implant gets one key column, id, constrained by both parents:

CREATE TABLE `implant` (
  `id` int NOT NULL COMMENT ':int32:',
  `implant_date` date NOT NULL COMMENT ':date:',
  PRIMARY KEY (`id`),
  CONSTRAINT `implant_ibfk_1` FOREIGN KEY (`id`) REFERENCES `mouse` (`id`) ...,
  CONSTRAINT `implant_ibfk_2` FOREIGN KEY (`id`) REFERENCES `probe` (`id`) ...
)

~lineage holds ('implant', 'id', 'djbugs_fkmerge.probe.id'). With the foreign keys in the opposite order, it holds mouse.id.

Consequences:

  • Joins with a declared parent fail. Implant * Mouse is rejected (different lineages (djbugs_fkmerge.probe.id vs djbugs_fkmerge.mouse.id)), although Mouse is a declared parent. Implant * Probe works.
  • Mice and probes are tied by number. An implant can only pair mouse N with probe N. A second implant for mouse 1 raises DuplicateError.
  • Control case. Recording with -> Session and -> Electrode, both carrying subject_id from Subject, merges correctly with lineage subject.subject_id. A fix must reject only differing lineages.

Expected

The declaration should be rejected with a DataJointError. The error should name the attribute and its two lineages, and suggest renaming one side, e.g. -> Probe.proj(probe_id='id').

Proposed fix

When a foreign key brings an attribute that is already in the heading, compare the two lineages:

  • Equal: keep the current behavior and share the column.
  • Different: raise.

Document the rule in table-declaration.md, and add tests for:

  • the rejected case;
  • the homologous merge;
  • the renamed workaround.
Ngôn ngữ chính
Python
Star
198
Fork
107
Merge trung bình
4 ngày 7 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.