Type.add()#
relationalai
#Type.add(
self,
*args: Type | TypeUnion | TypeIntersection,
**kwargs: Any,
) -> Instance
Defines new objects with the given Type
and returns an Instance
used to reference those objects in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
*args | Type , TypeUnion , TypeIntersection | Specifies additional types to which the object belongs. |
**kwargs | Any | Specifies properties of the object as key-value pairs. |
Returns#
An Instance
object.
Example#
Use Type.add()
to define a new object with the given type:
#import relationalai as rai
# =====
# SETUP
# =====
model = rai.Model("BooksModel")
# Declare a Book type.
Book = model.Type("Book")
# =======
# EXAMPLE
# =======
# Define a Book object with title and author properties.
with model.rule():
Book.add(title="Foundation", author="Isaac Asimov")
You may define an object with multiple types simultaneously by passing additional Type
objects as positional parameters:
#Fiction = model.type("Fiction")
SciFi = model.Type("SciFi")
with model.rule():
book = Book.add(Fiction, SciFi, title="Foundation", author="Isaac Asimov")
Only properties that uniquely identify the object should be passed to .add()
.
Non-identifying properties are set using the Instance.set()
method.
Properties passed to .add()
are hashed to create a unique internal identifier for the object.
When you select an Instance
object, the unique identifiers of the objects it references are returned:
#with model.query() as select:
book = Book()
response = select(book)
print(response.results)
# Output:
# book
# 0 iikm1rGdR3jWQtS2XVUZDg