cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

How to fine Primary Key of table dynamically using Aspects?

chrish
1-Newbie

How to fine Primary Key of table dynamically using Aspects?

I'm trying to build some globally used Shapes to be used by all new data tables in our implementation. One of which is a standard key generator. However, part of doing this is being able to dynamically identify the "key" of any given table.  ThingWorx has a common "Primary Key" Aspect when defining a data shape, so I'm sure there's a way to write a script that returns a STRING result in the form of the Field_Name who's aspect is boolean "Yes" for this Primary Key Aspect on any given table.

Advice??

1 ACCEPTED SOLUTION

Accepted Solutions

Hi Christopher, I've exactly did what you explained here. For your mater ( getting pk fields of a DataTable ) here it's my code:

( I have a property pk of type Infotable to store the PK fields ( data shape: FieldDefinition ), in order to not to calculate it each time you insert a row )

// -- Calculate Primary Keys

var dsName = me.GetDataShape();

var ds = DataShapes[dsName];

var fields = ds.GetDataShapeMetadataAsJSON().fieldDefinitions;

me.pk = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({

  infoTableName : "InfoTable",

  dataShapeName : "FieldDefinition"

});

me.pk.AddField({ name: "ordinal", baseType: "INTEGER" });

for (var property in fields) {

    if (fields.hasOwnProperty(property)) {

        if (fields[property].aspects) {

            if (fields[property].aspects.isPrimaryKey) {

                me.pk.AddRow({

                    name: property,

                    isPrimaryKey: true,

                    baseType: fields[property].baseType,

                    description: fields[property].desription,

                    dataShape: dsName,

                    ordinal: fields[property].ordinal

                });

            }

         }

    }

}

View solution in original post

1 REPLY 1

Hi Christopher, I've exactly did what you explained here. For your mater ( getting pk fields of a DataTable ) here it's my code:

( I have a property pk of type Infotable to store the PK fields ( data shape: FieldDefinition ), in order to not to calculate it each time you insert a row )

// -- Calculate Primary Keys

var dsName = me.GetDataShape();

var ds = DataShapes[dsName];

var fields = ds.GetDataShapeMetadataAsJSON().fieldDefinitions;

me.pk = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({

  infoTableName : "InfoTable",

  dataShapeName : "FieldDefinition"

});

me.pk.AddField({ name: "ordinal", baseType: "INTEGER" });

for (var property in fields) {

    if (fields.hasOwnProperty(property)) {

        if (fields[property].aspects) {

            if (fields[property].aspects.isPrimaryKey) {

                me.pk.AddRow({

                    name: property,

                    isPrimaryKey: true,

                    baseType: fields[property].baseType,

                    description: fields[property].desription,

                    dataShape: dsName,

                    ordinal: fields[property].ordinal

                });

            }

         }

    }

}

Top Tags