Creating a Data Model#
This tutorial covers creating a simple LinkAhead data model using PyLinkAhead, including Properties, RecordTypes, and inheritance hierarchies.
Concept |
PyLinkAhead |
|---|---|
Create a Property |
|
Create a RecordType |
|
Add a Property to a RecordType |
|
Insert a single entity into LinkAhead |
|
Insert multiple entities into LinkAhead |
|
Create a child RecordType |
|
Check inherited property importance |
|
Declare a list-typed Property |
|
Prerequisites#
Make sure you have completed First Steps with PyLinkAhead and have access to a writable LinkAhead instance. Import the client and configure your connection as shown in First Steps.
Properties and RecordTypes#
A Data Model in LinkAhead is built from RecordTypes and Properties; for a conceptual overview see LinkAhead Data Model.
Create a Property of datatype double:
temperature = db.Property(name="temperature", datatype=db.DOUBLE)
To insert multiple entities at once, add them to a Container and call .insert() once. The
following example builds a RecordType with three properties:
pressure = db.Property(name="pressure", datatype=db.DOUBLE)
duration = db.Property(name="duration", datatype=db.DOUBLE)
recordtype = db.RecordType(name="Experiment")
recordtype.add_property(temperature)
recordtype.add_property(pressure)
recordtype.add_property(duration)
container = db.Container()
container.extend([temperature, pressure, duration, recordtype])
container.insert()
print(recordtype.id)
101
Objects are created locally in Python and become part of the LinkAhead instance only after
.insert() is called. The printed value is the server-assigned ID; the actual value depends on the
state of your instance.
Inheritance of Properties#
For a conceptual explanation, see Property Inheritance.
To create a child RecordType that inherits all properties from a parent, use .add_parent() with
inheritance="all":
rt = db.RecordType(name="ControlledExperiment",
description="Experiment with controlled conditions")
rt.add_parent(name="Experiment", inheritance="all")
rt.insert()
print(rt.get_importance("temperature"))
RECOMMENDED
.get_importance() returns the importance level the server assigned to the inherited property.
RECOMMENDED is the default for properties inherited with inheritance="all".
Note
The inherited properties are only visible after insertion, since they are set by the LinkAhead server, not by the Python client.
List Properties#
To declare a Property that holds multiple values, use db.LIST() wrapping the element datatype:
p = db.Property(name="Measurements", datatype=db.LIST(db.DOUBLE))
p.insert()
The same db.LIST() syntax works in .add_property() when building a RecordType that uses the
property:
rt = db.RecordType(name="Experiment")
rt.add_property(name="Measurements", datatype=db.LIST(db.DOUBLE))
rt.insert()
The element type can be any scalar datatype (db.DOUBLE, db.INTEGER, db.TEXT, etc.) or a
RecordType name for properties that hold references to multiple records of that type. For example, a
Study RecordType whose experiments property references multiple Experiment records:
experiments_prop = db.Property(name="experiments", datatype=db.LIST("Experiment"))
experiments_prop.insert()
study_rt = db.RecordType(name="Study")
study_rt.add_property(experiments_prop)
study_rt.insert()
Summary#
This tutorial covered creating Properties and RecordTypes, inserting them as a group, setting up RecordType hierarchies using inheritance, and declaring list-typed Properties.
Continue with Inserting Records to insert, reference, and update records in LinkAhead.