Rows#
relationalai
#class Rows
Represents a set of rows of data.
Rows
objects are created using the as_rows()
function and can be used to pass data from Python to the model without having to define a Type
or use a dynamic rule or query context.
Methods#
Name | Description | Return Type |
---|---|---|
.__getitem__() | Gets an Expression object representing a column in the Rows object using subscript notation. | Expression |
.__getattr__() | Gets an Expression object representing a column in the Rows object using dot notation. | Expression |
Example#
Use as_rows()
to convert a list or dictionary of data to a Rows
object:
#import relationalai as rai
from relationalai.std import as_rows
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Company")
# =======
# EXAMPLE
# =======
# Define a Rows object from a list of dictionaries.
data_row = as_rows([
{"id": 1, "name": "Alice", "age": 30},
{"id": 2, "name": "Bob", "age": 45},
])
# Use data_row to define Person objects in the model.
with model.rule():
Person.add(id=data_row.id).set(name=data_row.name, age=data_row.age)
# Query the data.
with model.query() as select:
person = Person()
response = select(person.id, person.name, person.age)
print(response.results)
# id name age
# 0 1 Alice 30
# 1 2 Bob 45