Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

`table.get(column=value)` option for retrieving things not by their primary key

Abierto Apto para principiantes
#588 4 comentarios 1 reacción 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Tranquilo
Stack tecnológico
python, sqlite
Área
databases

Línea de trabajo

Comienza en sqlite_utils/db.py alrededor de la línea 1495, donde está definida la API existente Table.get(), y revisa su comportamiento actual de búsqueda posicional. Añade compatibilidad con la búsqueda de columnas mediante argumentos con nombre, siguiendo el ejemplo unique-name del issue y conservando el comportamiento de NotFoundError; después ejecuta las pruebas de base de datos existentes y añade cobertura para una búsqueda exitosa y para una fila inexistente.

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

Descripción

This came up working on this feature:

I have a table with this schema:

CREATE TABLE [collections] (
   [id] INTEGER PRIMARY KEY,
   [name] TEXT,
   [model] TEXT
);
CREATE UNIQUE INDEX [idx_collections_name]
    ON [collections] ([name]);

So the primary key is an integer (because it's going to have a huge number of rows foreign key related to it, and I don't want to store a larger text value thousands of times), but there is a unique constraint on the name - that would be the primary key column if not for all of those foreign keys.

Problem is, fetching the collection by name is actually pretty inconvenient.

Fetch by numeric ID:

try:
    table["collections"].get(1)
except NotFoundError:
    # It doesn't exist

Fetching by name:

def get_collection(db, collection):
    rows = db["collections"].rows_where("name = ?", [collection])
    try:
        return next(rows)
    except StopIteration:
        raise NotFoundError("Collection not found: {}".format(collection))

It would be neat if, for columns where we know that we should always get 0 or one result, we could do this instead:

try:
    collection = table["collections"].get(name="entries")
except NotFoundError:
    # It doesn't exist

The existing .get() method doesn't have any non-positional arguments, so using **kwargs like that should work:

https://github.com/simonw/sqlite-utils/blob/1260bdc7bfe31c36c272572c6389125f8de6ef71/sqlite_utils/db.py#L1495

Lenguaje dominante
Python
Estrellas
2.2k
Forks
172
Merge medio
9 min
PR fusionados (30 d)
1

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 simonw/sqlite-utils

Todos los issues de simonw/sqlite-utils

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.