Model Relationships

Introduction

Relationships define how different models connect to each other, allowing you to create complex data structures that reflect the connections in your research data. In Cnidarity, we've simplified relationships to make them more intuitive for researchers.

Instead of using traditional database terminology like "one-to-many" or "many-to-many," Cnidarity uses a more straightforward approach based on how many related records can be selected.

Relationship Types in Cnidarity

Cnidarity simplifies relationships into three intuitive categories based on the number of records that can be selected:

Single Relationship

A relationship where exactly one record from the target model can be selected.

Setting: Related Records Limit = 1

Example: An experiment belongs to one research project. When creating or editing an experiment record, you can select exactly one project from a dropdown menu.

Multiple (Limited) Relationship

A relationship where multiple records (up to a specified limit) from the target model can be selected.

Setting: Related Records Limit = 2 or more (any specific number)

Example: A research paper can have up to 5 authors. When creating a publication record, you can select up to 5 authors from a multi-select interface.

Multiple (Unlimited) Relationship

A relationship where any number of records from the target model can be selected.

Setting: Related Records Limit = 0

Example: A field site can have any number of observations. When creating a site record, you can select any number of observation records from a multi-select interface.

Creating Model Relationships

To create a relationship between models in your project:

  1. Navigate to the model where you want to define the relationship.
  2. Click on the "Relationships" tab in the model management interface.
  3. Click the "Create Relationship" button.
  4. Configure the relationship settings:
    • Target Model: Select the model you want to connect to.
    • Name: Enter a technical name for the relationship (e.g., "project_experiments").
    • Label: Enter a human-readable label (e.g., "Project Experiments").
    • Related Records Limit: Enter the maximum number of records that can be selected (1 for single, 0 for unlimited, or any specific number).
    • Required: Check this box if the relationship must be filled when creating records.
  5. Select display attributes (optional) to determine which fields from the related model should be shown.
  6. Click "Save" to create the relationship.
The "Related Records Limit" is the key setting that determines the type of relationship. Set it to 1 for a single selection, 0 for unlimited selections, or any other number for a specific limit.

Selecting Related Records

Once you've defined relationships between models, you can connect specific records when creating or editing records:

Single Record Selection

For relationships with a limit of 1, you'll see a dropdown selector:

  • Click the dropdown to see available records from the related model
  • Select one record from the list
  • The selected record will be connected to your current record

Multiple Record Selection

For relationships with a limit of 0 or greater than 1, you'll see a multi-select interface:

  • Click in the selection field to see available records
  • Select multiple records by clicking on them
  • You can remove selections by clicking the "x" next to each selected record
  • If a limit is set, you won't be able to select more than the specified number of records

Search

You can search within the record selection interface by typing. This helps you quickly find specific records when you have many options to choose from.

Setting Display Attributes

When creating or editing a relationship, you can select which attributes from the target model should be displayed when viewing related records:

  1. In the relationship creation or edit form, find the "Display Attributes" section.
  2. Select the attributes that best identify the related records.
  3. These attributes will be shown when displaying related records in the record view.

For example, if you have a "Publication" model with "title", "journal", and "publication_date" attributes, you might select "title" and "journal" as display attributes for better identification when viewing related publications.

Managing Existing Relationships

After creating relationships, you can manage them from the Relationships tab of your model:

Editing Relationships

  • Click the "Edit" button next to a relationship.
  • Modify any settings, including the name, label, and related records limit.
  • Update display attributes as needed.
  • Save your changes.

Deleting Relationships

  • Click the "Delete" button next to a relationship.
  • Confirm the deletion.

Warning

Deleting a relationship will remove all connections between records based on that relationship. This action cannot be undone, so use it with caution.