# To a single record

{% hint style="success" %}
This is the official documentation of the `@forestadmin/agent` Node.js agent.
{% endhint %}

Relationships that point to a single record are displayed in the GUI as links.

Once configured, they can be used in [charts](https://docs.forestadmin.com/user-guide/dashboards/charts), [filters](https://docs.forestadmin.com/user-guide/getting-started/master-your-ui/the-table-view#add-one-or-several-filters), [scopes](https://docs.forestadmin.com/user-guide/collections/scopes), and [segments](https://docs.forestadmin.com/user-guide/collections/segments).

![Many-to-One relation in the Table View](https://3861847666-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UN5oBJhgzLadOqi7jx6%2Fuploads%2Fgit-blob-a9207cfd9b233ac51d964a5a3523f9012e60e3b4%2Frelationships-single-link.png?alt=media)

{% hint style="info" %}
Note that, for performance reasons when sorting a Table View on customizer-defined relations, Forest Admin will always sort on the primary key of the related collection.
{% endhint %}

### Many-to-One relations

Many-to-One relations are by far the most common type of relation: many records from a Collection are attached to another Collection record.

Think about countries and towns: a town belongs to a single country, but each country can have multiple towns.

```javascript
agent.customizeCollection('towns', collection =>
  collection.addManyToOneRelation('country', 'countries', {
    foreignKey: 'country_id',
    foreignKeyTarget: 'id', // Optional (uses `country` primary key by default)
  }),
);
```

### One-to-One relations

In a one-to-one relation, there is a one-to-one mapping between records in 2 Collections. The relation can be unset for some records, but no record from the first Collection can be linked to more than one record in the other Collection.

Think about cities and mayors: A city can have at most one mayor, and each mayor belongs to a single city.

{% hint style="warning" %}
Take note that the inverse of a `one-to-one` is a `many-to-one`.

This may seem counter-intuitive: the side of the relation which should be configured as `many-to-one` is the one that carries the foreign key.
{% endhint %}

```javascript
// Configure one side of the relation ...
agent.customizeCollection('mayors', collection => {
  collection.addOneToOneRelation('city', 'cities', {
    originKey: 'mayor_id',
    originKeyTarget: 'id', // Optional (uses `mayors` primary key by default)
  });
});

// ... and the other one.
agent.customizeCollection('cities', collection => {
  // ⚠️ Not 'OneToOne'
  collection.addManyToOneRelation('mayor', 'mayors', {
    foreignKey: 'mayor_id',
    foreignKeyTarget: 'id', // Optional (uses `mayors` primary key by default)
  });
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/single-record.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
