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

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:

storageType sub attribute, value string; 

field sub entity,
    owns storageType;

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

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:
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.

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.

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

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

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.