--- last_review: "2026-05-06" last_reviewer: "s.kaviani" documented_code: [ ] --- ```{tags} advanced-user, tutorial, schema ``` % Issue: https://gitlab.indiscale.com/caosdb/src/linkahead-docs/-/issues/72 # 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 | `db.Property(name=..., datatype=...)` | | Create a RecordType | `db.RecordType(name=...)` | | Add a Property to a RecordType | `.add_property()` | | Insert a single entity into LinkAhead | `.insert()` | | Insert multiple entities into LinkAhead | `db.Container().extend([...]).insert()` | | Create a child RecordType | `.add_parent(name=..., inheritance="all")` | | Check inherited property importance | `.get_importance()` | | Declare a list-typed Property | `db.LIST()` | ## Prerequisites Make sure you have completed [First Steps with PyLinkAhead](first_steps.md) and have access to a writable LinkAhead instance. Import the client and configure your connection as shown in [First Steps](first_steps.md). ## Properties and RecordTypes A {term}`Data Model` in LinkAhead is built from RecordTypes and Properties; for a conceptual overview see [LinkAhead Data Model](/explanation/general/data-model.md). Create a Property of datatype double: ```{literalinclude} /.assets/tests/tutorials/pylib/test_data_insertion.py :start-after: "# ---- LINK-1 ----" :end-before: "# ---- LINK-2 ----" :language: python :dedent: ``` To insert multiple entities at once, add them to a `Container` and call `.insert()` once. The following example builds a RecordType with three properties: ```{literalinclude} /.assets/tests/tutorials/pylib/test_data_insertion.py :start-after: "# ---- LINK-2 ----" :end-before: "# ---- LINK-3 ----" :language: python :dedent: ``` ```{code-block} text :caption: Output: :class: text-output 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](/explanation/general/data-model.md#property-inheritance). To create a child RecordType that inherits all properties from a parent, use `.add_parent()` with `inheritance="all"`: ```{literalinclude} /.assets/tests/tutorials/pylib/test_data_insertion.py :start-after: "# ---- LINK-3 ----" :end-before: "# ---- LINK-4 ----" :language: python :dedent: ``` ```{code-block} text :caption: Output: :class: text-output 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: ```{literalinclude} /.assets/tests/tutorials/pylib/test_data_insertion.py :start-after: "# ---- LINK-26 ----" :end-before: "# ---- LINK-27 ----" :language: python :dedent: ``` The same `db.LIST()` syntax works in `.add_property()` when building a RecordType that uses the property: ```{literalinclude} /.assets/tests/tutorials/pylib/test_data_insertion.py :start-after: "# ---- LINK-27 ----" :end-before: "# ---- LINK-28 ----" :language: python :dedent: ``` 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: ```{literalinclude} /.assets/tests/tutorials/pylib/test_data_insertion.py :start-after: "# ---- LINK-28 ----" :end-before: "# ---- LINK-29 ----" :language: python :dedent: ``` ## 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](inserting_records.md) to insert, reference, and update records in LinkAhead.