Public join(allow_nullable_pk=True) puts NULL into a primary key

Aperta
#1,749 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
40/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
databases

Direzione di ricerca

Read the public join() signature and the left-join check in src/datajoint/expression.py (around lines 343 and 383-387), then the key-merging logic in src/datajoint/heading.py around lines 735-754. Follow the single internal caller in Aggregation.create (expression.py ~1367) to see what it actually needs, and read the docs at reference/specs/primary-keys.md lines 250-306 plus the extend() docs. Done means allow_nullable_pk is no longer reachable from the public join() while aggregation's left join still works, verified by the reproduction in the issue becoming impossible and the existing join/aggregation tests passing. Note this is an API decision between the two options, so confirm the preferred direction with a maintainer before coding.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Summary

join(other, left=True, allow_nullable_pk=True) is part of the public signature, and it returns results whose primary key contains NULL. The heading still marks those key attributes as non-nullable, and later operators accept the result without complaint. A public call can therefore break the guarantee that every query result has a non-null primary key.

The parameter exists for internal use: its only caller is aggregation with exclude_nonmatching=False. The docstring and the docs (reference/specs/primary-keys.md, around lines 250–306) call it "primarily internal" and say "Users should generally avoid this parameter".

Reproduction (master @ 207068d, MySQL 8.0)

@schema
class Subject(dj.Manual):
    definition = """
    subject_id : int32
    ---
    species : varchar(32)
    """

@schema
class Session(dj.Manual):
    definition = """
    session_id : int32
    ---
    -> [nullable] Subject
    session_date : date
    """

@schema
class Trial(dj.Manual):
    definition = """
    -> Session
    trial_id : int32
    ---
    response_time : float32
    """

Subject.insert([(1, "mouse"), (2, "rat")])
Session.insert([dict(session_id=10, subject_id=1, session_date="2026-01-01"),
                dict(session_id=11, subject_id=None, session_date="2026-01-02")])
Trial.insert([(10, 1, 0.5), (10, 2, 0.6)])

Session.join(Trial, left=True)                           # rejected (A must determine B)
q = Session.join(Trial, left=True, allow_nullable_pk=True)
q.primary_key                                            # ['session_id', 'trial_id']
q.keys()   # [{'session_id': 11, 'trial_id': None}, {'session_id': 10, 'trial_id': 1}, ...]

q.heading reports trial_id as nullable=False. A nullable secondary foreign key gives the same result: Subject.join(Session, left=True, allow_nullable_pk=True) has primary key ['subject_id', 'session_id'] and contains {'subject_id': 2, 'session_id': None, ...}. These results are accepted as operands of * and & without complaint.

Expected

No public operator should be able to produce a primary key containing NULL. Make the parameter private:

  • Option 1: pass it through an internal-only argument or helper used by Aggregation.create.
  • Option 2: remove it from join() and build the aggregation's left join directly.

The extend() docs already describe it as "an internal mechanism".

Related: #1708. Its audit says that join keys always come from the operands' keys, and that dj.U is the only operator that puts non-key attributes into a key. This parameter is a second way to get NULL into a key.

Lingua principale
Python
Stelle
198
Fork
107
Merge medio
4g 7h
PR unite (30g)
12

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di datajoint/datajoint-python

Tutte le issue di datajoint/datajoint-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.