patito.duckdb.Relation.get

Relation.get(*filters, **equalities)

Fetch the single row that matches the given filter(s).

If you expect a relation to already return one row, you can use get() without any arguments to return that row.

Raises:

RuntimeError – RuntimeError is thrown if not exactly one single row matches the given filter.

Parameters:
  • filters (str) – A conjunction of SQL where clauses.

  • equalities (Any) – A conjunction of SQL equality clauses. The keyword name is the column and the parameter is the value of the equality.

Returns:

A Patito model representing the given row.

Return type:

Model

Examples

>>> import patito as pt
>>> import polars as pl
>>> df = pt.DataFrame({"product_id": [1, 2, 3], "price": [10, 10, 20]})
>>> relation = pt.duckdb.Relation(df).set_alias("my_relation")

The .get() method will by default return a dynamically constructed Patito model if no model has been associated with the given relation:

>>> relation.get(product_id=1)
my_relation(product_id=1, price=10)

If a Patito model has been associated with the relation, by the use of Relation.set_model(), then the given model will be used to represent the return type:

>>> class Product(pt.Model):
...     product_id: int = pt.Field(unique=True)
...     price: float
...
>>> relation.set_model(Product).get(product_id=1)
Product(product_id=1, price=10.0)

You can invoke .get() without any arguments on relations containing exactly one row:

>>> relation.filter(product_id=1).get()
my_relation(product_id=1, price=10)

If the given predicate matches multiple rows a MultipleRowsReturned exception will be raised:

>>> try:
...     relation.get(price=10)
... except pt.exceptions.MultipleRowsReturned as e:
...     print(e)
...
Relation.get(price=10) returned 2 rows!

If the given predicate matches zero rows a RowDoesNotExist exception will be raised:

>>> try:
...     relation.get(price=0)
... except pt.exceptions.RowDoesNotExist as e:
...     print(e)
...
Relation.get(price=0) returned 0 rows!