# What is a the right way to model a table-fields meta schema structure?

**URL:** <https://forum.typedb.com/t/what-is-a-the-right-way-to-model-a-table-fields-meta-schema-structure/271>\
**Category:** TypeQL\
**Created:** [8 December 2022 13:21 UTC](https://forum.typedb.com/t/what-is-a-the-right-way-to-model-a-table-fields-meta-schema-structure/271 "2022-12-08T13:21:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![KrishnaPG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/krishnapg/32/26_2.png) [@KrishnaPG](https://forum.typedb.com/u/KrishnaPG)\
**Post date:** [8 December 2022 13:21 UTC](https://forum.typedb.com/t/what-is-a-the-right-way-to-model-a-table-fields-meta-schema-structure/271/1 "2022-12-08T13:21:27Z")

</div>

I need to model the concept of: “`table has multiple fields, each field can have different validation rules based on its type`”.

Not sure what is the best way to achieve this. I have this to start with:

```auto
storageType sub attribute, value string; 

field sub entity,
    owns storageType;

```

Now, how to model the `table has many fields` concept? I did this:

```auto
table sub entity,
    plays tbl-fld-relation:table,
    owns name;

tbl-fld-relation sub relation,
    relates table,
    relates field,

```

**_Question 1_:**

- Is the above the best way or should I just say:

```auto
table sub entity, 
    owns field;

```

**_Question 2_:**

- How to model: `field has different validation rules based on its type?`

For example, if `field.storageType == "string"` then that field should have `string validation rules`, if `field.storageType == "number"` then that field should have `number validation rules` etc.

```auto
minLength sub attribute, value long;
maxLength sub attribute, value long;
minValue sub attribute, value long;
maxValue sub attribute, value long;

   str-validation-rules sub entity,
       owns minLength,
       owns maxLength;

   num-validation-rules sub entity,
       owns minValue,
       owns maxValue;       

```

How to mix the above kind of validation rules with the `field` based on its attributed value?

If it helps, the background is: I am trying to model the Postgres database metadata (the `tables, fields, foreign keys, primary kets etc. all`) in TypeDB;

Any help in modelling it correctly would be of great help. Thank you.

---

<div class="post-metadata">

**Author:** ![james.williams](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/james.williams/32/49_2.png) [@james.williams](https://forum.typedb.com/u/james.williams)\
**Post date:** [8 December 2022 15:38 UTC](https://forum.typedb.com/t/what-is-a-the-right-way-to-model-a-table-fields-meta-schema-structure/271/2 "2022-12-08T15:38:34Z")

</div>

> Question 1: is the above the best way or should I just say […]

You’ll need a relation to have an entity be related to another entity. Entities can’t `owns` other entities. You may have to do some of your own work to programatically enforce the cardinality as TypeDB doesn’t currently support it, see the issue: [Introduce cardinality restriction · Issue #34 · vaticle/typeql · GitHub](https://github.com/vaticle/typeql/issues/34)

> Question 2: How to model: `field has different validation rules based on its type?`

TypeDB has a very good answer for this: a feature that is itself named rules. See the documentation page: [TypeDB | Docs \> Home \> Documentation overview](https://docs.vaticle.com/docs/schema/rules)

Another hint that may help you: you can make use of TypeDB’s strong typing system to avoid having to attach attributes in the form of `storageType "string"`. For example, you could define an entity type `stringField` that inherits from `field`. From this, you can infer that the string validation is the appropriate type of validation to be used on any field of type `stringField`.
