Reject constant defaults on primary-key attributes

Aberta Para iniciantes
#1,712 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 5 dias

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
84/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
mysql, python
Domínio
databases

Direção de pesquisa

Comece por compile_attribute em declare.py e acompanhe como os valores padrão dos atributos de chave primária são classificados. Adicione testes para valores padrão constantes de chaves, valores padrão CURRENT_TIMESTAMP de chaves e valores padrão inalterados de atributos secundários. O trabalho estará concluído quando valores padrão constantes de chaves forem rejeitados na declaração com um erro que nomeie o atributo e informe aos usuários que removam o valor padrão, enquanto os valores padrão gerados e secundários continuem sendo aceitos.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.
Linguagem predominante
Python
Estrelas
198
Forks
103
Merge médio
5d 1h
PRs com merge (30d)
12

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de datajoint/datajoint-python

Todas as issues de datajoint/datajoint-python

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.