Model.Type()#
#Model.Type(name: str, source: Optional[str] = None) -> Type
Declares a new Type in the model.
Use the optional source
parameter to specify a Snowflake table with source data for objects with the given type.
Parameters#
Name | Type | Description |
---|---|---|
name | str | The name of the type. Must begin with a Unicode letter or an underscore followed by one or more Unicode letters, underscores, or numbers. |
source | str | (Optional) The name of a Snowflake table or view with source data for objects with the given type. (Default: None .) |
Returns#
A Type
object.
Supported Snowflake Data Types#
The following Snowflake data types are supported in tables and views passed to the source
parameter:
Example#
Use Model.Type()
to declare a new type in the model:
#import relationalai as rai
model = rai.Model("MyModel")
# Declare a Person type.
Person = model.Type("Person")
# Use the Person type to define Person objects.
with model.rule():
Person.add(name="Alice")
Person.add(name="Bob")
When you declare a type with the source
parameter, objects with that type are created using data from the specified Snowflake table.
Each row in the table corresponds to an object with the given type.
Properties of the objects are inferred from the columns in the table, using the lowercased column names as property names.
For instance, suppose you have a Snowflake table named people
with columns Name
and Age
with the following data:
Name | Age |
---|---|
Alice | 25 |
Bob | 30 |
In the following model, the Person
type is defined with source data from the people
table:
#import relationalai as rai
model = rai.Model("MyModel")
# Declare a Person type with source data from a Snowflake table named "people".
# <db> and <schema> are placeholders for the Snowflake database and schema names.
Person = model.Type("Person", source="<db>.<schema>.people")
# Properties of Person objects are inferred from the columns in the "people" table
# and are accessible as attributes of the Person type using lowercased column names.
with model.query() as select:
person = Person()
response = select(person.name, person.age)
print(response.results)
# name age
# 0 Alice 25
# 1 Bob 30