Reject constant defaults on primary-key attributes

Abierto Apto para principiantes
#1,712 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 5 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
84/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
mysql, python
Área
databases

Línea de trabajo

Empieza con compile_attribute en declare.py y sigue cómo se clasifican los valores predeterminados de los atributos de clave primaria. Añade cobertura para valores predeterminados constantes de claves, valores predeterminados CURRENT_TIMESTAMP de claves y valores predeterminados de atributos secundarios sin cambios. La tarea estará completa cuando los valores predeterminados constantes de claves se rechacen durante la declaración con un error que nombre el atributo e indique a los usuarios que eliminen el valor predeterminado, mientras que los valores predeterminados generados y secundarios sigan aceptándose.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
Python
Estrellas
198
Forks
103
Merge medio
5 d 1 h
PR fusionados (30 d)
12

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de datajoint/datajoint-python

Todos los issues de datajoint/datajoint-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.