Property#

relationalai
#class Property

Instances of Property represent properties of a given Type and are analogous to a column of a SQL table. You create them by accessing the property as a Type attribute. Property objects represent the property itself, not its value. For accessing values, see InstanceProperty.

Attributes#

NameTypeDescription
.is_multi_valuedboolWhether the property is multi-valued.

Methods#

NameDescriptionReturns
.declare()Declare a single-valued property.None
.has_many()Declare a multi-valued property.None

Example#

You can create a Property object by accessing it as an attribute of a Type:

#import relationalai as rai

model = rai.Model("books")
Book = model.Type("Book")

with model.rule():
    Book.add(title="Foundation", author="Isaac Asimov")

# Access the 'title' property of the Book type.
print(type(Book.title))
# Output:
# <class 'relationalai.dsl.Property'>

Property objects cannot be used as variables in a rule or query. Attempting to do so raises an exception:

#with model.query() as select:
    title = Book.title
    author = Book.author
    title == "Foundation"  # This raises an exception.

To declare a property as multi-valued, use the .has_many() method. This is required for properties derived from Snowflake tables or views where a column contains multiple rows for the same primary key:

#import relationalai as rai

model = rai.Model("students")

# Create a Student type from the 'students' table in the 'sandbox.public'
# Snowflake schema. The table has `id`, `course`, and `grade` columns and
# may have multiple rows for each student enrolled in multiple courses.
Student = model.Type("Student", source="sandbox.public.students")

# Declare that the 'course' property is multi-valued.
Student.course.has_many()

# What courses is student 1 enrolled in?
with model.query() as select:
    student = Student(id=1)
    response = select(student.course)

print(response.results)
# Output:
#         course
# 0  Mathematics
# 1      History
# 2      Science

Without declaring Student.course.has_many(), the course property would be treated as single-valued, and the query would return only one course per student.

You can add multiple values to a property using the InstanceProperty.add() or InstanceProperty.extend() methods:

#import relationalai as rai

# Create a model named "pets" with Person and Pet types.
model = rai.Model("pets")
Person = model.Type("Person")
Pet = model.Type("Pet")

# Declare that the 'pets' property of the Person type is multi-valued.
Person.pets.has_many()

with model.rule():
    alice = Person.add(name="Alice")
    # Add one Pet to Alice's 'pets' property.
    alice.pets.add(Pet.add(name="Fluffy", type="cat"))

    bob = Person.add(name="Bob")
    # Add two Pets to Bob's 'pets' property.
    bob.pets.extend([
        Pet.add(name="Spot", type="dog"),
        Pet.add(name="Rex", type="dog")
    ])

In this example, .has_many() is optional. .add() and .extend() automatically handle the creation of multi-valued properties.

See Also#