Property.declare()#
relationalai
#Property.declare() -> None
Declare a single-valued property.
Example#
Use .declare()
to declare a single-valued property used by a Type
:
#import relationalai as rai
# Create a model named "MyModel" with a Person type.
model = rai.Model("MyModel")
Person = model.Type("Person")
# Declare Person.name as a single-valued property. NOTE: This is optional
# in most cases.
Person.name.declare()
# Check that the Person.name property is single-valued.
print(Person.name.is_multi_valued)
# False
# Define Person and Pet objects.
with model.rule():
Person.add(name="Alice")
# NOTE: Person.add() automatically creates a single-valued 'name' property
# if Person.name.declare() is missing.
.declare()
is optional in most cases.
Calling Type.add()
or InstanceProperty.set()
automatically creates a single-valued property if it hasn’t been declared.
You can use .declare()
to avoid the UninitializedPropertyException
raised when trying to access a property that has never been set.
This can happen, for example, when you define a Type
from a Snowflake table and access a property corresponding to a column containing only NULL
values:
#Employee = model.Type("Employee", source="<db>.<schema>.employees")
# Suppose the 'middle_name' column in the 'employees' table contains only NULL
# values, since it's an optional field and no one has provided their middle name.
# Accessing Employee.middle_name without declaring it raises an
# UninitializedPropertyException, but you can declare it to avoid the exception:
Employee.middle_name.declare()