Reject constant defaults on primary-key attributes

Aperta Adatta ai principianti
#1,712 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à
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
mysql, python
Ambito
databases

Direzione di ricerca

Inizia da compile_attribute in declare.py e segui il modo in cui vengono classificati i valori predefiniti degli attributi di chiave primaria. Aggiungi test per i valori predefiniti costanti delle chiavi, i valori predefiniti CURRENT_TIMESTAMP delle chiavi e i valori predefiniti invariati degli attributi secondari. Il lavoro è completo quando i valori predefiniti costanti delle chiavi vengono rifiutati in fase di dichiarazione con un errore che nomina l’attributo e indica agli utenti di rimuovere il valore predefinito, mentre i valori predefiniti generati e secondari continuano a essere accettati.

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

Descrizione

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.
Lingua principale
Python
Stelle
198
Fork
103
Merge medio
5g 1h
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.