# Create a Collection Relationship in Specify 7

**URL:** <https://speciforum.org/t/create-a-collection-relationship-in-specify-7/1837>\
**Category:** Configuration & Installation\
**Tags:** Specify-7\
**Created:** [June 25, 2024, 4:42pm UTC](https://speciforum.org/t/create-a-collection-relationship-in-specify-7/1837 "2024-06-25T16:42:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grant](https://yyz1.discourse-cdn.com/flex029/user_avatar/speciforum.org/grant/32/4255_2.png) [@Grant](https://speciforum.org/u/Grant)\
**Post date:** [June 25, 2024, 4:42pm UTC](https://speciforum.org/t/create-a-collection-relationship-in-specify-7/1837/1 "2024-06-25T16:42:14Z")

</div>

> 📖 This guide explains how to create a `CollectionRelType` form, how to show the plugins on the forms, and how to query using the Collection Relationship.

> [!info] Read This First
> 
> **[🔖 Collection Relationships](https://speciforum.org/t/collection-relationships/182)**

* * *

Before you can create a Collection Relationship in Specify, you need to define a `CollectionRelType` view definition to create a Collection Relationship.

If you are unfamiliar with editing forms, please [watch and read our documentation on the subject](https://speciforum.org/t/editing-forms-in-specify-7/1557) before proceeding.

## Create CollectionRelType Form

First, you will need to either create a new form definition or navigate to an existing one available to your user in [App Resources](https://speciforum.org/t/app-resources-in-specify-7/582).

Once there, check “Show All Tables” to display all tables in the Specify model. Find “Collection Relationship Type” in the list.

 ![Collection Relationship Type highlighted in list of tables](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/d/d37926091970d39c52397cfa7a6d4b7966d53842.png)

Once selected, click the Create button to make a new view definition for the table.

Rather than choosing one of the default views, click New to create one from scratch.

 ![Create New Collection Relationship Type form](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/4/472b6dc85132c6bf12fadc5697b08977c4516b5c.png)

You will need to leave the name as “CollectionRelType”.

 ![Form Definition Title for CollectionRelType|](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/d/dd439ff555b4802df6be28a03f7dc2224ef797a6.png)

Once the new view definition is created, copy the following XML and replace the contents of the new XML:

```xml
<viewdef
            type="form"
            name="CollectionRelType"
            class="edu.ku.brc.specify.datamodel.CollectionRelType"
            gettable="edu.ku.brc.af.ui.forms.DataGetterForObj"
            settable="edu.ku.brc.af.ui.forms.DataSetterForObj">
            <desc><![CDATA[The Collection Relationship Type form]]></desc>

            <columnDef>100px,2px,195px,5px,86px,2px,210px,5px,74px,2px,146px,15px,p:g</columnDef>
            <columnDef os="lnx">115px,2px,195px,5px,125px,2px,210px,5px,125px,2px,131px,15px,p:g</columnDef>
            <columnDef os="mac">130px,2px,215px,5px,138px,2px,260px,5px,138px,2px,155px,15px,p:g</columnDef>
            <columnDef os="exp">p,2px,min(p;220px),5px:g,p,2px,p,5px:g,p,2px,200px,p,p:g</columnDef>
            <rowDef auto="true" cell="p" sep="2dlu"/>

            <rows>
                <row>
                    <cell type="label" labelfor="1"/>
                    <cell type="field" id="1" name="name" uitype="text"/>
                    <cell type="label" labelfor="2"/>
                    <cell type="field" id="2" name="remarks" uitype="text"/>
                </row>
                <row>
                    <cell type="label" labelfor="4"/>
                    <cell type="field" id="4" name="leftSideCollection" uitype="querycbx" initialize="name=CollectionRelationship" isrequired="true" colspan="6"/>
                </row>
                <row>
                    <cell type="label" labelfor="5"/>
                    <cell type="field" id="5" name="rightSideCollection" uitype="querycbx" initialize="name=CollectionRelationship" isrequired="true" colspan="6"/>
                </row>
            </rows>
        </viewdef>

```

After this, you need to add `/specify/view/collectionreltype/new/` to the end of your Specify installation’s base URL (e.g. `https://sp7demofish.specifycloud.org/specify/view/collectionreltype/new`) and navigate to the new Collection Rel Type form.

Give the `CollectionRelType` a name

Use the :search_: (search) icon to the right of the Left Side Collection and Right Side Collection query combo boxes to search for the collections you want to make the relationship between.

> [!Important]  
> If the collections are in a different discipline, you will need to set both query combo boxes to use the collection within the current discipline. Then, switch collections, navigate to the Collection Relationship Type record, and set the appropriate “Right Side” or “Left Side” collection.
> 
> This is due to the ‘Query Builder’ system being scoped to the current collection.

 ![Query Builder for Collections](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/3/330f7a53791571c9cc662162bb23d08064f67895.png)

Once the collections are set, you can click Save to create the relationship.

 ![Completed form](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/c/c8a94eea4f0ecf02359d4f3dfe998e27fe541369.png)

## Form Definition Usage

In the forms, these relationships can be referenced directly in the Collection Relationship form plugins.

### [CollectionRelOneToManyPlugin](https://speciforum.org/t/editing-forms-in-specify-7/1557#collectionrelonetomanyplugin-41)

 ![CollectionRelOneToManyPlugin](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/a/a222cadbcb24200d3b529cdef04c6e3d613fd789.png)

In the XML,

```xml
<cell type="label" label="Tissues:" initialize="align=left;"/>
<cell type="field" id="colrel" uitype="plugin" name="this" initialize="name=CollectionRelOneToManyPlugin;relname=Has Tissue;" colspan="9"/>

```

### [ColRelTypePlugin](https://speciforum.org/t/editing-forms-in-specify-7/1557#colreltypeplugin-42)

![ColRelTypePlugin](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/a/aed3461ccac532abcb05228c268962412ca96d28.png)

In the XML,

```xml
<cell type="label" label="Tissues:" initialize="align=right;"/>
<cell type="field" id="colrel" uitype="plugin" name="this" initialize="name=ColRelTypePlugin;relname=Has Tissue;"/>

```

## Building a Query

Now that you can associate records in a Collection Relationship, you will need to modify the [Schema Config](https://speciforum.org/t/using-the-schema-config-in-specify-7/535) for the discipline(s) involved in the relationship.

_From [Collection Relationships](https://speciforum.org/t/collection-relationships/182#collection-relationships-1):_

> #### On a database level, the Collection Object Relationship sides are:
> 
> **Source:** Left Side  
> **Destination:** Right Side

To search and find data from objects linked via the `CollectionRelOneToManyPlugin` (source to destination, e.g. ‘voucher → tissue’), you will need to unhide the relationship field `leftSideRels` on the `collectionobject` table in the schema.

 ![LeftSideRels Field in Schema](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/7/7125d81867a8d72b60fee6428f717995af99b38e.png)

Once it is unhidden, you can query across `Collection Object --> Left Side Rels --> Right Side --> Collection Object` to retrieve data from the linked COs, even if they are in another collection.

 ![Left Side Rels](https://canada1.discourse-cdn.com/flex029/uploads/specifysoftware/original/2X/2/2cda2fd2a1eef197dfe3bb5876948bfef01bee36.png)

In a similar way, to search and find data from objects linked in the `ColRelTypePlugin` (destination to source, e.g. ‘tissue → voucher’), you will need to unhide the relationship field `rightSideRels` on the `collectionobject` table in the schema.

Once it is unhidden, you can query across `Collection Object --> Right Side Rels --> Right Side --> Collection Object` to retrieve data from the source CO.
