# Examples or guides for creating types, fields, relations?

**URL:** <https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491>\
**Category:** API & Programming\
**Created:** [November 7, 2022, 10:08pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491 "2022-11-07T22:08:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 7, 2022, 10:08pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/1 "2022-11-07T22:08:29Z")

</div>

Hi, I’d like to try to programmatically create an entire schema “from scratch” via the API. Browsing the API docs, I think I understand how to create types, but I don’t quite understand how to create relations between types.

For example, from reading the API docs I see two apparently relevant commands: `schema.field/create` and `schema.type/create` — am I supposed to create the types first, and then create the relation fields in a subsequent command, and then add the fields to the types in yet another subsequent command? Or is it possible to create the relations on the fly while creating the types, by including relations in the list of type fields?

I’m not sure that makes sense, but another way to approach this would be: it’d be super helpful if I could see some example code of calling the API in order to create types and create relations between those types. I mean not just simplistic examples that show the syntax of each command, but realistic examples based on use cases that show workflow, order of operations, etc.

Thank you!

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 7, 2022, 10:18pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/2 "2022-11-07T22:18:30Z")

</div>

You might want to check this out

> [@"Meta-fibery" app](https://community.fibery.io/t/meta-fibery-app/1138):
>
> This is a bit of a weird one. After reading this: I got to thinking about using fibery for a self-describing system, and I have made an app that does some interesting stuff (I think). My app is called Metamodel, and the idea behind it is that Metamodel describes the types/fields/relations to be found in another app, called Model. This will sound a bit crazy, but bear with me… Metamodel contains three types, called Type, Field and Relation. Each entity of the type Type defines a type that …

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 7, 2022, 11:23pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/3 "2022-11-07T23:23:58Z")

</div>

Thanks @Chr1sG!

I found the JavaScript code in that Metamodel app/template thing, and reading it over, it _seems_ as though the idea is to create the types first (and while doing so one can only include fields that already exist) then create additional fields, then create relations.

Is that right?

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 7, 2022, 11:49pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/4 "2022-11-07T23:49:30Z")

</div>

> [@avi](#):
>
> one can only include fields that already exist

Not sure what you mean by this.  
Basically, you create the necessary types (databases) with mandatory fields plus any desired primitive fields (number, text, date etc.)  
Then you can create relations between types by adding pairs of fields, representing the links as viewed from one database to the other.

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 7, 2022, 11:53pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/5 "2022-11-07T23:53:51Z")

</div>

> [@Chr1sG](#):
>
> Not sure what you mean by this.

I’m probably not using the right terminology, or just not making sense, because I don’t yet understand Fibery’s internal model.

> [@Chr1sG](#):
>
> Basically, you create the necessary types (databases) with mandatory fields plus any desired **primitive** fields (number, text, date etc.)

Emphasis mine — aha, that’s helpful, you can include fields when creating a type, but only _primitive_ fields — I didn’t see that anywhere.

> [@Chr1sG](#):
>
> Then you can create relations between types by adding pairs of fields, representing the links as viewed from one database to the other.

Makes sense, thanks!

Although… it’s a little surprising we have to manually/mechanically express both sides of the relation. NBD, just curious: if the two fields that make up a relation are mirror images of each other, why not specify one side and have the API derive the other side? Is it so we can customize the name on each side?

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 8, 2022, 12:09am UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/6 "2022-11-08T00:09:23Z")

</div>

> [@avi](#):
>
> If the two fields that make up a relation are mirror images of each other, why not specify one side and have the API derive the other side?

Well, the two fields are not ‘mirror’ images, since a relationship can be 1-1, 1-n, n-1 or n-n.  
The easiest way to define the relationship cardinality is to define each end of the relationship as either 1 or n.  
And yes, it also allows you to choose the name of the relationship as viewed from either direction.

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 8, 2022, 12:10am UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/7 "2022-11-08T00:10:36Z")

</div>

makes sense, thanks!

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 9, 2022, 2:14pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/8 "2022-11-09T14:14:03Z")

</div>

BTW, is there any documentation on the concept of a non-primitive field? I’m a little fuzzy on when I’d want/need to use one.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 9, 2022, 2:27pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/9 "2022-11-09T14:27:05Z")

</div>

Strictly speaking, ~~they~~ primitives are described here:

> **[Fibery API](https://api.fibery.io/#field)**
>
> The Fibery API provides a way to integrate Fibery with your external systems and automate routine tasks.

From a user’s point of view, they appear on the UI as:

Text,  
Number,  
Date,  
Checkbox,  
URL,  
Email,  
Phone,  
Emoji

Non-primitives are all the rest:  
rich-text, single selects, multi selects, relations, lookups, formulas, assignees, workflow, files, documents, whiteboards, comments, avatar

See my later post for more detail.

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 9, 2022, 3:00pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/10 "2022-11-09T15:00:58Z")

</div>

Thanks, but I’m not sure I understand. That list is almost but not quite the same as the list in the API docs of primitive field types:

```auto
fibery/int
fibery/decimal
fibery/bool
fibery/text
fibery/email
fibery/emoji
fibery/date
fibery/date-time
fibery/date-range
fibery/date-time-range
fibery/uuid
fibery/rank
fibery/json-value

```

So could you please help me understand the difference between primitive field types and non-primitive types? What even is a non-primitive field type? Is that the correct term? What are some an examples of non-primitive field types? Is there a list in the docs somewhere? Or are they composed ad-hoc of the primitive types?

Or maybe, just looking at the structure of the API docs, maybe the non-primitive field types are relations, single-selects, and rich-text fields, and maybe that’s it?

Sorry but I’m finding the docs terse and confusing.

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 9, 2022, 4:26pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/11 "2022-11-09T16:26:53Z")

</div>

Sorry, my previous post was providing info on primitive fields, not non-primitives, my mistake. I will update the post.

**Primitives**  
Any number field (number, currency or percent) is stored as either fibery/int or fibery/decimal depending on the settings chosen by the user:

A checkbox field is stored as fibery/bool

A text field (not rich text field) is stored as fibery/text  
Phone number, email and URL fields are also stored as fibery/text (they are just shown differently on UI)

A date field, can be either fibery/date, fibery/date-time, fibery/date-range or fibery/date-time-range depending on the selections made by the user.

**Non-primitives**  
‘Non-primitive’ fields includes all the rest 😉 :  
rich-text, single selects, multi selects, relations, lookups, formulas, assignees, workflow, files, documents, whiteboards, comments, avatar

One way to think about it is to think of primitive (basic) fields as the most simple characteristics of an entity (which can be stored as a single value).  
In the real world, for a human being, this is things like: date of birth, height, name, shoe-size

Whereas the non-primitive (advanced) fields are the more complex characteristics, which can’t be stored as a single number/string etc.  
In the real world, for a human being, this might be things like: siblings, educational achievement, number of children, etc.

There is no standard way of describing how a non-primitve field works, but I think for your case, the most important ones to understand are relations and select fields

> **[Fibery API](https://api.fibery.io/#relation-entity-collection-field)**
>
> The Fibery API provides a way to integrate Fibery with your external systems and automate routine tasks.

> **[Fibery API](https://api.fibery.io/#single-select-field)**
>
> The Fibery API provides a way to integrate Fibery with your external systems and automate routine tasks.

(a multi-select is just a many-to-many version of a single-select field)

Assignees behave like a many-to-many relation to the User db  
Workflow behaves like a single-select field

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 18, 2022, 3:26pm UTC](https://community.fibery.io/t/examples-or-guides-for-creating-types-fields-relations/3491/12 "2022-11-18T15:26:42Z")

</div>

That was super helpful — thank you!
